# Trying to use ForEach() method and confused what I'm doing wrong

**URL:** <https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804>\
**Category:** PowerShell Help\
**Created:** [December 17, 2018, 4:30pm UTC](https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804 "2018-12-17T16:30:36Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![gregory-suvalian](https://avatars.discourse-cdn.com/v4/letter/g/9d8465/32.png) [@gregory-suvalian](https://forums.powershell.org/u/gregory-suvalian)\
**Post date:** [December 17, 2018, 4:30pm UTC](https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804/1 "2018-12-17T16:30:36Z")

</div>

Hello,

I have List of Objects returned from AzureRM cmdlet and I’m trying just to output them to output stream and it’s not working for some reason.

> <https://gist.github.com/artisticcheese/7467954280c1de6f3ceb2d2f0b1647c6>

---

<div class="post-metadata">

**Author:** ![palhan](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@palhan](https://forums.powershell.org/u/palhan)\
**Post date:** [December 17, 2018, 5:45pm UTC](https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804/2 "2018-12-17T17:45:15Z")

</div>

It appears that you cannot do that on a generic list…

```
$a = [System.Collections.Generic.List[int]]::new()
$a.AddRange([int[]](1,4,7,10))

$a.ForEach

OverloadDefinitions                      
-------------------                      
void ForEach(System.Action[int] action)
```

This is the native .NET method, and it returns nothing. It gets called like this:

```
$a.ForEach({ Write-Host ("Value: {0}" -f $args[0]) })

Value: 1
Value: 4
Value: 7
Value: 10
```

A couple of ways to make it behave…

```
$a.GetEnumerator().ForEach({ $_ })

foreach($x in $a) {
    $x
}
```

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 17, 2018, 8:17pm UTC](https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804/3 "2018-12-17T20:17:49Z")

</div>

This should work as well:

```
$nicInfo.IpConfigurations | ForEach{$_}
```

---

<div class="post-metadata">

**Author:** ![peter-jurgens-2](https://avatars.discourse-cdn.com/v4/letter/p/3bc359/32.png) [@peter-jurgens-2](https://forums.powershell.org/u/peter-jurgens-2)\
**Post date:** [December 19, 2018, 1:29am UTC](https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804/4 "2018-12-19T01:29:36Z")

</div>

I’m fairly certain the .foreach{} method doesn’t support $\_, only $PSItem. Can’t explain why but that’s my experience.

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [December 19, 2018, 8:22am UTC](https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804/5 "2018-12-19T08:22:59Z")

</div>

Nope, it supports `$_` and `$PSItem`. In the PowerShell code that handles those things, there _is_ a difference, but they’re handled the same when parsing script blocks, so there aren’t any situations where you can use one and not the other.

_However_, there are situations where they aren’t used by themselves. Some of the methods on the List class (including its `ForEach()` method, which differs from the usual PowerShell ETS method that can be used on almost anything that doesn’t have it’s own `ForEach()`-named method) can’t, for example, and you have to either use `$args` or define a `param()` block for them. For example:

```
using namespace System.Collections.Generic
$List = [List[int]]::new()
$List.AddRange( @(1, 2, 3, 4, 5) )

$List.FindAll(
    {
        param($Item)
        $Item -gt 3
    }
)

# output
4
5
```

---

<div class="post-metadata">

**Author:** ![palhan](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@palhan](https://forums.powershell.org/u/palhan)\
**Post date:** [December 19, 2018, 10:55am UTC](https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804/6 "2018-12-19T10:55:26Z")

</div>

PSItem gets resolved to \_ internally, so with other words: it’s an alias.

---

<div class="post-metadata">

**Author:** ![ta11ow](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/ta11ow/32/131_2.png) [@ta11ow](https://forums.powershell.org/u/ta11ow)\
**Post date:** [December 19, 2018, 11:44am UTC](https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804/7 "2018-12-19T11:44:40Z")

</div>

Well, sort of. The code itself _does_ distinguish clearly between them as different tokens, but in terms of how the function they’re treated identically from what I’ve seen.

---

<div class="post-metadata">

**Author:** ![palhan](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@palhan](https://forums.powershell.org/u/palhan)\
**Post date:** [December 19, 2018, 12:17pm UTC](https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804/8 "2018-12-19T12:17:35Z")

</div>

They have this in the source code for Core:

```
internal static string GetUnaliasedVariableName(string varName)
{
	return varName.Equals(SpecialVariables.PSItem, StringComparison.OrdinalIgnoreCase)
			   ? SpecialVariables.Underbar
			   : varName;
}

internal static class SpecialVariables
{
	internal static bool IsUnderbar(string name) { return name.Length == 1 && name[0] == '_'; }
	internal const string PSItem = "PSItem"; // simple alias for $_
	internal const string Underbar = "_";
}
```

So if varName == “PSItem” then return “\_”

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [December 19, 2018, 3:15pm UTC](https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804/9 "2018-12-19T15:15:21Z")

</div>

So is there any difference between $\_ and $psitem?

---

<div class="post-metadata">

**Author:** ![palhan](https://avatars.discourse-cdn.com/v4/letter/p/96bed5/32.png) [@palhan](https://forums.powershell.org/u/palhan)\
**Post date:** [December 19, 2018, 3:24pm UTC](https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804/10 "2018-12-19T15:24:54Z")

</div>

There is a difference in the amount of characters you have to write. In functionality, no.

---

<div class="post-metadata">

**Author:** ![js2010](https://avatars.discourse-cdn.com/v4/letter/j/ebca7d/32.png) [@js2010](https://forums.powershell.org/u/js2010)\
**Post date:** [December 19, 2018, 3:28pm UTC](https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804/11 "2018-12-19T15:28:17Z")

</div>

Would you know why ‘-and’ and ‘-or’ have the same precedence?

---

<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:33pm UTC](https://forums.powershell.org/t/trying-to-use-foreach-method-and-confused-what-im-doing-wrong/11804/12 "2024-05-16T20:33:33Z")

</div>

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


