Rectangle 27 27

I think the easiest way to think about this variable like input parameter in lambda expression in C#. I.e. $_ is similar to x in x => Console.WriteLine(x) anonymous function in C#. Consider following examples:

1,2,3 | ForEach-Object {Write-Host $_}
1,2,3 | Where-Object {$_ -gt 1}

And compare this with C# syntax using LINQ:

var list = new List<int> { 1, 2, 3 };
list.ForEach( _ => Console.WriteLine( _ ));
1
2
3
list.Where( _ => _ > 1)
    .ToList()
    .ForEach(s => Console.WriteLine(s));
2
3

In C# don't use _ as input parameter in lambda. _ is sometimes used when the parameters is ignored, use x instead. stackoverflow.com/questions/424775/

What does $_ mean in PowerShell? - Stack Overflow

powershell