r/PowerShell • u/iamelloyello • 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
102
u/GiulianoM Nov 17 '22
$_ represents the object(s) passed from the previous step in the pipeline.
In your sample, $_ would refer to the process(es) returned from Get-Process.
And $_.VM means the VM (Virtual Memory?) property of the process.
So by piping Get-Process into Format-Table, for each Process found, it will make a table with the Process Name, and the Process VM formatted as an expression with the column name "VM(MB)", Virtual Memory in Megabytes.