Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

For some reason the % alias for ForEach-Object throws an exception when using the ( $Thing in $Things) syntax while the ForEach alias works fine.

Here are two examples:

Using the % alias:

$ints = @(1, 2, 3, 4, 5)
% ($i in $ints)
{Write-Host $i}

This fails with the error Unexpected token 'in' in expression or statment.

Using the ForEach alias:

$ints = @(1, 2, 3, 4, 5)
foreach ($i in $ints)
{Write-Host $i}

This succeeds without issue.

Why is there a difference if they are both aliases of ForEach-Object?

share|improve this question

1 Answer 1

up vote 12 down vote accepted

These are two different things:

% is the alias for the cmdlet ForEach-Object and foreach is also the alias for the cmdlet ForEach-Object... and foreach is a looping statement which does not work with pipelining.

As written, your first command expands to:

ForEach-Object ($i in $ints) {
  Write-Host $i 
}

...which isn't valid syntax for the ForEach-Object cmdlet.

When foreach appears, as with your second command, it is interpreted as a foreach statement, not the cmdlet alias. In this case the syntax is valid and loops as expected.

You can compare the differences with get-help about_Foreach and get-help ForEach-Object. This blog post Essential PowerShell: Understanding foreach also does a nice job explaining.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.