Discover how PowerShell's automatic helpers can do the thinking for you and save hours of tedious work!
Why Automatic variables ($_, $PSVersionTable) in PowerShell? - Purpose & Use Cases
Imagine you need to process a list of files one by one and check your PowerShell version manually every time you run a script. You open the console, type commands repeatedly, and try to remember the current item you are working on.
This manual way is slow and tiring. You might forget which file you just processed or which version of PowerShell you are using. It's easy to make mistakes or waste time typing the same info again and again.
Automatic variables like $_ and $PSVersionTable help by holding useful info for you. $_ always points to the current item in a loop, so you don't have to track it yourself. $PSVersionTable shows your PowerShell version details instantly, so you can adapt your script easily.
foreach ($item in $list) { Write-Host $item; } Write-Host "PowerShell version: 5.1"
$list | ForEach-Object { Write-Host $_ }
Write-Host $PSVersionTable.PSVersionWith these automatic variables, your scripts become smarter and faster, handling data and environment info without extra effort.
When cleaning up files, $_ lets you act on each file automatically, and $PSVersionTable helps you check if your script needs to change for different PowerShell versions.
Automatic variables save you from tracking details manually.
$_ represents the current item in loops or pipelines.
$PSVersionTable gives quick access to your PowerShell version info.