Contrary to common belief, PowerShell doesn’t just make up variables whenever it feels like it. Just a few years ago, I would have swore it did though. Take this script as example:
Get-ChildItem \\SERVER\SHARE\ -recurse -file | get-acl | Where-Object {$_.Owner -match "Joseph" }
We start by recursively listing every file on our share. These files are piped (|) to the get-acl command. Finally, we filter the results with the where-object command. Out of nowhere, we throw in $_.Owner variable! Now I have two questions – where did the $_ come from and how did the .Owner get added to it?
What Does $_. Mean in PowerShell?
As a simplified explanation, $_ symbolizes This. In the example above, you can think about the Where-Object segment like this: Where-Object {$THISFILE.Owner -match “Joseph” }. To quote this TechNet article,
$_ got its name because the underscore character resembles a pipeline character (|) lying on its side, implying that it contains whatever was piped in.
In essence, $_ is a dynamic variable storing the current information being piped to it! In the example above, $_ is the file. That clears things up a bit but…





