r/PowerShell • • Nov 17 '22

Question ELI5 "$_"

Attempting my way through "PS through a month of lunches" only experience being my A+.

They are just glossing over "$_"

Here is an example from the book.

Can someone break this down for me? They attempt to in the book but don't go in depth enough, so I would have no idea how to use this in a real scenario:

Get-Process | Format-Table Name, @{name='VM(MB)';expression={$_.VM / 1MB
➥ -as [int]}}

I just feel like this book goes leaps and bounds over what you just learned about. I just went from learning how to formatting a list to whatever... that is. Again, they *attempt* to explain what is going on here, but I have no idea what "{" are used for, why you would use them, when you would use them, etc.

132 Upvotes

61 comments sorted by

View all comments

1

u/TheDraimen Nov 18 '22

This took me a bit to grasp when first learning but became something I miss as I do more C# . Best I could explain it is it means the current object. Best way I grasped it was take an array of 7 objects representing a car. If you want to know the color of each car you would first do something like $cars =get-allcars" and then would do a foreach($car in $cars){ $car.color}. With $_ the foreach would look for like $cars | foreach {$_.color}. After you get used to that calling each object in an array of a pipeline becomes a lot easier.