# Foreach vs Foreach-object

**URL:** <https://forums.powershell.org/t/foreach-vs-foreach-object/7776>\
**Category:** PowerShell Help\
**Created:** [December 31, 2016, 9:51am UTC](https://forums.powershell.org/t/foreach-vs-foreach-object/7776 "2016-12-31T09:51:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maheshadate](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@maheshadate](https://forums.powershell.org/u/maheshadate)\
**Post date:** [December 31, 2016, 9:51am UTC](https://forums.powershell.org/t/foreach-vs-foreach-object/7776/1 "2016-12-31T09:51:40Z")

</div>

Hi,  
I encountered a weird problem with foreach loop. My understanding is foreach is alias of foreach-object - which I can confirm by using get-alias command.  
However if I run the following command, I get different results with foreach & foreach-Object, below is simple conde what I have tried, can someone explain why there is a difference in behaviour, is it a bug or expected behaviour?

$list = “A”, “B”, “C”, “D”

PS C:&gt; ForEach ($item in $list) {$item}  
A  
B  
C  
D  
PS C:&gt; ForEach-Object ($item in $list) {$item}  
_At line:1 char:23_

 
_- ForEach-Object ($item in $list) {$item}
-_ 

_```
                  ~~_

_```_

 

_Unexpected token ‘in’ in expression or statement.  
At line:1 char:22_

 
_- ForEach-Object ($item in $list) {$item}
-_ 

_```
                 ~_

_```_

 

_Missing closing ‘)’ in expression.  
At line:1 char:31_

 
_- ForEach-Object ($item in $list) {$item}
-_ 

_```
                          ~_

_```_

 

_Unexpected token ‘)’ in expression or statement.  
+ CategoryInfo : ParserError: (🙂 , ParentContainsErrorRecordException  
+ FullyQualifiedErrorId : UnexpectedToken_

---

<div class="post-metadata">

**Author:** ![juancrl](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@juancrl](https://forums.powershell.org/u/juancrl)\
**Post date:** [December 31, 2016, 10:52am UTC](https://forums.powershell.org/t/foreach-vs-foreach-object/7776/2 "2016-12-31T10:52:45Z")

</div>

You are partially right and partially wrong 🙂

“Foreach” are just 7 letters that PowerShell will have to parse in **two** different ways, depending on the context.

**1.** As you correctly mentioned, they could be an alias for a Cmdlet called Foreach-Object. Other alias is %, but you’ve already found them with Get-Alias.  
To be understood as such a cmdlet, you have to use it in a pipeline. In your example :

```
$list | Foreach-Object -Process { $_ }
```

This could be abbreviated with the alias and using default parameters to

```
$list | Foreach { $_ }
```

But I wouldn’t recommend it. Do not use aliases, they could have been modified to do a different thing…

**2.** “Foreach” can also be a keyword, a language structure used to enumerate any object which can be enumerated.  
The syntax is totally different (similar to foreach in C#, for example), and is NOT translated to a cmdlet.

```
foreach ($element in $list) { $element }
```

Notice in this case you don’t use $\_ but a variable you explicitly declare ($element in this example)

Unfortunately they chose “foreach” for both the keyword and the alias, which makes things slightly difficult.

Homework : Foreach-Object has also a -Begin and -End parameter…

---

<div class="post-metadata">

**Author:** ![maheshadate](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@maheshadate](https://forums.powershell.org/u/maheshadate)\
**Post date:** [January 1, 2017, 1:56pm UTC](https://forums.powershell.org/t/foreach-vs-foreach-object/7776/3 "2017-01-01T13:56:31Z")

</div>

Many thanks Ruiz, that explains the change in behaviour… Using alias as foreach for foreach-object confused me a bit

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:39pm UTC](https://forums.powershell.org/t/foreach-vs-foreach-object/7776/4 "2024-05-16T20:39:40Z")

</div>

Closed on May 16, 2024, 8:39pm UTC


