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.
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.
34
u/aptechnologist Nov 17 '22
so its kinda like using a variable, within a pipeline, but without having ever specifically defined it, and it's gone as soon as that command is done?
50
u/GiulianoM Nov 17 '22
Yes.
Anything that starts with a $ is a variable.
Something like "$VARIABLE" would be a manually created variable.
But $_ is what's called an Automatic Variable.
There are others as well:
6
9
u/Mysterious-Ad-1541 Nov 17 '22
Do you have any trainings I can use to test this? I have read this over and over and do not understand.
14
u/Necoras Nov 17 '22
Consider this array:
$myArray = @(1,2,3,4,5,6)You can do:
$myArray | ForEach-Object { Write-Host "$_" }This is equivalent to:
ForEach ($myVar in $myArray){ Write-Host "$myVar" }Personally I prefer the latter (and indeed, my team would reject the first on a code review) because it's a bit clearer, and "$myVar" is harder to miss than "$_" somewhere in the middle of a long line of code. It's also clearer what it is in a non-contrived example. "$myVar" isn't meaningful, but "$service" or "$process" or "$file" is.
1
u/retr0baD Nov 18 '22
Is $myVar a default variable in PowerShell for referencing array items?
3
u/ZaRx2048 Nov 18 '22
No, in a foreach you create a variable to represent each object/element in the array. You could call it $foo in $myArray. Example if you use $myFruits as an array you could use foreach($fruit in $myFruits)…
When I was in the early stages of learning the get-help and about pages were very helpful. Look up powershell about arrays.
2
1
u/wisym Nov 17 '22
So it's for golfing the code?
3
3
u/ka-splam Nov 18 '22
So it's for golfing the code?
No more than your apostrophe is for golfing English.
-5
u/Necoras Nov 17 '22
It's there for people who are used to writing/pasting in code snippets and shell commands. It's not really well suited to a well developed code base. Rather it's for someone who wants a collection of magic strings that are short and easy to paste in and get a desired effect out. (just my 2 cents.)
5
u/wdomon Nov 18 '22
$_ is a fundamental concept in Powershell and while it can be replaced by an explicit parameter, it is used in every variation of size and complexity in a script there is. Personally, I’ve written millions of lines of Powershell (literally) with modules in the hundreds of thousands of lines; this is a bad take.
3
u/wonkifier Nov 18 '22
I work on the command-line a TON, stringing commands together to get tasks done.
Being able to use
get-things | %{dosomething to it} | %{sendsomewhere}without having to open a notepad or something to pop in a multiline embedded foreach structure is one of the main reasons I love powershell.2
u/JeremyLC Nov 18 '22
It's also memory use vs performance. Foreach (statement) is faster, but consumes more memory (and breaks the pipeline) because the entire collection of objects has to be allocated in RAM, whereas Foreach-Object ($_) processes objects in the pipeline as they're allocated. (See also Dr. scripto
1
u/Namelock Nov 18 '22
It's definitely subjective; I find the order of your latter example to be confusing. And dear god, the amount of people that have abysmal naming schemes...
I prefer to <parentitem><foreach><childitem> because my brain can't do <foreach><childitem><parentitem>. Especially when people do:
ForEach ($object in $objects){Write-Host "$object"}Naming is often too similar from child to parent...
17
Nov 17 '22
Contains the current object in the pipeline object. You can use this variable in commands that perform an action on every object or on selected objects in a pipeline.
12
u/aptechnologist Nov 17 '22
microsofts documentation has gotten so damn good - maybe its always been solid but lately everything i've landed on has been updated in the last 3 to 6 months, and it's always my first stop for ANYTHING. Companies should look at ms learn as an example on how to build out a product with proper documentation & support
16
Nov 17 '22
Microsoft seems to go in cycles from making fantastic documentation to "lol, wut?" I recall the SQL 2000 Books Online, which shipped with SQL 2000. It was like the Magnum Opus of some SQL documentation savant. As far as I can tell, the creators were promptly murdered by Microsoft, as the documentation quality nose dived hard after that.
PowerShell seems to be in a good cycle at the moment. Hopefully the authors are keeping an eye over their shoulders.
5
u/da_chicken Nov 17 '22
SQL Server's documentation is still extremely high quality. But it's not as in-depth or detailed with side topics and white papers as it was between SQL Server 2000 and SQL Server 2008 R2. But it's still excellent, especially compared to other RDBMS vendors.
IMO, however, their best documentation at the moment is .Net 6/7. That's been very good since about .Net Framework 3.0/3.5. Every time I look at doc, I'm sad that it's not more like .Net's.
4
Nov 17 '22
their best documentation at the moment is .Net 6/7. That's been very good since about .Net Framework 3.0/3.5. Every time I look at doc, I'm sad that it's not more like .Net's.
Ya, relevant to this being /r/PowerShell, I find myself using the .Net documentation constantly. Just this morning, I wanted a quick way to shift a timestamp to UTC in PowerShell and landed on the TimeZoneInfo page. Full class, properties and methods all laid out in an easy to navigate interface.
1
u/archcycle Nov 18 '22
I hate recent SQL documentation - as a non-SQL guru that is. The KB articles contain so many references to jargon that would take maybe 5 words in parentheses to fully explain so they all require non-experts to go outside the articles to understand the articles, when most of the time it looks like anyone who knew all of the concepts wouldn't be reading the article in the first place.
It's like the documentation writers want to prove they are awesome at SQL more than they want to just to point you in the right direction for troubleshooting SQL's "Permanent Intermittent Disconnect 0x5004d&335r4 in un-pipeable replica transaction - Click here for troubleshooting but actually a redirect to Microsoft.com/BuyWindows10" or whatever it is today.
1
u/da_chicken Nov 18 '22
KB articles? WTF are you on about? This is the SQL Server documentation.
1
u/archcycle Nov 18 '22
Knowledge Base is not always pejorative. Sorry if i offended you.
1
u/da_chicken Nov 19 '22
No, I know that. I just don't call MS KB articles on error codes the RDBMS documentation.
2
u/elevul Nov 17 '22
That's because volunteers contribute to it... They basically opensourced documentation
1
-1
12
u/razzledazzled Nov 17 '22
$_ is known as the “pipeline variable” and is an alias for $PSItem
12
u/NotNotWrongUsually Nov 17 '22
<pedantic> $PSItem is an alias for $_ ! </pedantic>$PSItem didn't join the party until PowerShell 3. Beyond historical accuracy this trivia knowledge has no value at all.
9
u/PinchesTheCrab Nov 17 '22
I was fortunate to attend Don Jone's last class, and I asked if we should use $PSItem or $_, since $PSItem is easier to google, and both he and the PS team lead agreed that $PSItem is the alias, and that adding it has only complicated documentation.
For me personally I found $_ hard to google, but that was back in like 2010 when I started learning PS, and after talking to Don, I've dropped $PSItem altogether.
1
1
6
u/GiulianoM Nov 17 '22
Also, the bit inside the @{ } bracket section is called a Calculated Property:
https://mcpmag.com/articles/2017/01/19/using-powershell-calculated-properties.aspx
@{
name='VM(MB)';
expression=
{
$_.VM / 1MB -as [int]
}
}The above code creates a column for Format-Table Named "VM(MB)", and its value is calculated in the Expression:
$_.VM divided by 1 Megabyte, and then converted to an Integer (-as [int]), which removes the remainder number bits after the period. e.g. 250 MB instead of 250.123456 MB
5
u/SomeCynicalBastard Nov 17 '22 edited Nov 21 '22
Same as $PSItem. Contains the current object in the pipeline object. You can use this variable in commands that perform an action on every object or on selected objects in a pipeline.
I would add that $_ is also a placeholder in ForEach-Object 'loops'.
As for the curly braces, the main uses are:
${name}, to use a variable. Usually we just write$name;@{key1=val1;key2=val2;...}, to define a HashTable. You don't need semicolons if you use linebreaks;{ ... }, to define a script block. This is just a block of PowerShell code.
In your example, Format-Table takes a HashTable as aparameter, with keys 'name' and 'expression'. Expression takes a script block as a value. This script block is evaluated for each object that is passed down the pipeline to Format-Table, and $_ is automatically filled with the object passed down the pipeline.
6
u/sysiphean Nov 17 '22
Small correction:
$_ (or $PSItem) is a placeholder in ForEach-Object loops, but not foreach loops.
1
u/SomeCynicalBastard Nov 21 '22
Thanks, you're absolutely right. Wouldn't even make sense in a foreach loop :)
2
u/jpbras Nov 17 '22
"${name}, to use a variable. Usually we just write $name;"
Just a little note on this...
You want to use ${when variable have spaces} when the variable have spaces (or any special character) and ${c:\variableInAFile.txt) if you want to place the variable on any PSDrive recognizable by Powershell.
That's why {} are used on variables.
1
Nov 18 '22
Is chapter one still basically... Heres how to find man pages, now go RTFM.
2
u/iamelloyello Nov 18 '22
It's chapter 11, actually.
1
Nov 18 '22
Good to see they switch it up some between versions.
2
u/iamelloyello Nov 18 '22
You must be fun to work with. What's the point of this sub if to not ask questions?
1
3
u/technicalityNDBO Nov 17 '22
I'm not an expert, so someone correct me if I'm wrong.
You're aware that powershell deals with objects. Objects have a bunch of properties tied to them.
Get-aduser -identity "iamelloyello" would show me your user object and a couple of default properties to display.
If I wanted to deal with just a single property of an object, you'd put a period after the object and then the name of the property. Like iamelloyello.department .
And remember that get-aduser -identity "iamelloyello" is essentially the same thing as just your username. The same way that 7-4 is essentially the same thing as saying "3". And just like in math, you can put an equation or formula in parentheses to group it together: (7-4). In Powershell we can do (get-aduser -identity "iamelloyello").department
Now do a cmdlet to fetch a BUNCH of ad users, and you want to do something to the "Department" property of EACH of them. You don't type out the individual cmdlet for each user. $_ is the placeholder. $_.department would be like "get me the department for each of the adusers I just put into the pipeline.
2
u/Temilit Nov 17 '22
Not quite off topics buut ..
I really try to avoid using format-table at all times, i want to keep my data organized in objects so i can work with the data easier in the next steps, from your example you could get pretty much the same return without "Destroying" the object form and keep the ability to leverage the power it brings with the following:
just use select-object instead of format-table
get-process | select-object name,@{name='VM(MB)';expression={$_.VM / 1MB -as [int]}}
1
Nov 17 '22
I hate the kind of shorthand they have used in this script, I don't actually know what the script does of the top of my head
However, I can attempt to explain my understanding of "$_"
$_ is a placeholder variable/object.
Look at this simple script:
$ComputerList = Get-ADComputer -Filter * -SearchBase *
$ComputerList = ForEach-Object {
if (Test-Connection -ComputerName $_.Name -Count 1 -Quiet) {
Write-Host $_.Name +"OK"
} else {
Write-Host $_.Name +"PING FAILED"
}
}In this case $_ will change each loop and be the next object in the $Computer list variable, $_.Name refers to the Name attribute of the current object.
As far as I know $_ will always refer back to a dataset used in a function.
1
u/allthetrouts Nov 17 '22
Commenting to say that if you are going to filter for everything, why also add searchbase and accept all values there? The filter parameter shown is showing everything anyways.
1
Nov 18 '22
I wrote the quick script from memory, I often use searchbase to select an OU, and forget that it isn't needed.
0
Nov 17 '22
[deleted]
1
Nov 18 '22
Sorry, I was thinking about the @, but now that I have read about it in the thread, I do remember it is used to define an array. I just got confused.
1
u/groovel76 Nov 17 '22
A way to look at it is like this.
Get-Process | Format-Table Name, @{name='VM(MB)';expression={$_.VM / 1MB -as [int]}}
Get-Process -pipelinevariable prcs | Format-Table Name, @{name='VM(MB)';expression={$prcs.VM / 1MB -as [int]}}Basically there needs to be something after the dollar sign. If no name is given, PowerShell goes with an underscore.
And you are still formatting a list, the "name/Expression" just allows you to further manipulate the data, should the default formatting not be useful to you.
1
Nov 17 '22
Thanks for the pipelinevariable I often have to store the $_ in an another variable in order to be able to do a select-object in my calculated property expression without loosing the initial $_
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.
1
1
u/Namelock Nov 18 '22
For a newbie: $_ is the current item you are looping on.
I much prefer to use it over the 'conventional' method. It's harder to spot, but easier to understand. Convention is essentially doing <foreach><childitem><parentitem> when you could instead <parentitem><foreach><childitem>.
Likewise, You'd be surprised how often I find examples like this:
Foreach-Object (object in objects){object.Totals}
When I think this is much easier to comprehend:
the_csv | Foreach-Object {$_.Totals}
Definitely a personal preference, but hope this helps.
1
u/dieth Nov 18 '22
$_ is the representation of the individual item of the loop you're looping over. In this case you're using a loop to sort the list by the size of the VM(MB) stat of the object. It's just a special shortcut variable so you don't have to declare one.
in other languages you'll see a declared variable like i below, instead of $_.
for i in range(5):
print(i)1
u/toddklindt Nov 18 '22
I first saw $_ explained as the "fill in the blank" variable, and that always stuck with me. Like the others have said, it's whatever is getting passed from the pipeline, or whatever object in the collection you're currently working on.
1
u/ShadowMasterTexas Nov 19 '22
$_ is the pipeline value.
For example, did Get-process, it would have
Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName
in the object. Lets say you only wanted handles that were above 1000 in number.
A command might be Get-process | where {$_.handles -gt 1000}
This would give you every row of data where the handle number was over 1000.
In your example, it says to use the value of VM from the pipeline. (didn't show VM when I ran my test, so that might be the problem here as well).
So, lets say you were instead looking at a folder directory.. In my case:
PS C:\Users\media> Get-ChildItem
Directory: C:\Users\media
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 10/14/2020 9:48 PM .android
d----- 6/23/2022 11:30 PM .ms-ad
d----- 11/23/2019 8:55 PM .oracle_jre_usage
d----- 6/20/2020 5:25 PM .swt
d----- 11/10/2021 6:20 PM .thinkorswim
d-r--- 3/9/2021 5:51 PM 3D Objects
d----- 3/9/2021 5:42 PM AppData
d-r--- 3/9/2021 5:51 PM Contacts
d-r--- 5/22/2021 2:16 PM Creative Cloud Files
d-r--- 11/18/2022 9:21 AM Desktop
d-r--- 11/17/2022 8:53 PM Documents
d----- 10/5/2022 2:34 AM Downloads
but you only want to list things that had an C in them.
PS C:\Users\media> Get-ChildItem | where {$_.name.contains("c")}
Directory: C:\Users\media
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 11/23/2019 8:55 PM .oracle_jre_usage
d-r--- 3/9/2021 5:51 PM 3D Objects
d-r--- 3/9/2021 5:51 PM Contacts
d-r--- 11/17/2022 8:53 PM Documents
d-r--- 9/16/2022 10:17 PM Music
d----- 11/19/2022 12:50 AM music2
d-r--- 11/13/2022 8:14 AM Pictures
d-r--- 3/9/2021 5:51 PM Searches
Each of these have the letter C so that were all returned.
Does that help at all?
1
u/OkProfessional8364 Nov 17 '22
Maybe a simple example will help you grasp the $PSItem concept.
$numbers = @(1,2,3)
$numbers | %{ $_ * 2 }
# 2
# 4
# 6
62
u/wickedang3l Nov 17 '22 edited Nov 17 '22
It's less confusing than it looks at first glance but it does deserve a more thorough explanation. This is a cognitively dense operation that is made more confusing by PowerShell's heavy syntax requirements.
Let's go through it section by section:
The Get-Process cmdlet gathers the processes and the pipe character passes the collection of objects into the pipeline to be addressed by other cmdlets.
The Format-Table cmdlet receives the pipeline collection of objects for further processing. You have used a positional parameter to define one of the properties, Name, that you want to be in your formatted table. Name is a property that every object in the collection that was passed to Format-Table will possess because every object discovered by the Get-Process cmdlet will have a Name property.
Of course, you have others that need to be addressed.
This is a dense statement with a high cognitive load but what is happening will make more sense. @{} is what PowerShell uses to create a hash table data structure. Hash tables are key-value pairs (In the instance of my account, think accountName-wickedang3l). That's all they do.
Now that you understand the @{} part, let's look at this closer.
Since we're working with a hash table, we know we are working with a key-value pair.
name='VM(MB)' is defining a custom name for the key of this key-value pair. VM(MB) is not a property that is automatically created by the Get-Process cmdlet; you, the user, are defining a property that you want to have in this table by doing this operation and this is the name that is being given to it.
expression={$_.VM / 1MB -as [int]} is denoting a subexpression that must be calculated in order to give you the value in the key-value pair. In this instance, it is indicating that it wants the VM property of every object in the collection passed to Format-Table to be divided by 1MB and for the derived value of that operation to be cast as an integer data type.
$_ is a stand-in for every object in the collection that was passed to Format-Table. $_.VM is a way to call upon the VM (VirtualMemorySize64) property for every object in the collection that was passed to Format-Table.
If you execute Get-Process | Get-Member, you will be able to see that VM is an AliasProperty that is available to you via the Get-Process cmdlet.