11 PowerShell Automatic Variables You Should Know

Updated: Sep 16

Automatic variables are one of the fundamental things you should know about PowerShell. When I was learning PowerShell, I kept creating complicated scripts to handle things easily handled by those automatic variables.

In this post, I will cover a few of the automatic variables I find most useful. This is in no way an exhaustive list. You can find the complete list of automatic variables here.


$?

This variable is very useful when you don't want to get into a whole try/catch setup. It contains the execution result of the last command run. It will be $true if it succeeded and $false if it did not.

One thing to note about this variable is that it will not give you whether or not an executable ran successfully (exit code 0) unless that executable is one of the native commands (such as pingcmd, etc.).



$_/$PSItem

These two variables are the same variables, just different names. They both return the current object being processed by a pipe. For example, if you pipe an array of objects to a ForEach-Object$_ will represent one of the objects at any given time.

That being said, starting in PowerShell v4, the -PipelineVariable common parameter allows you to name your own variable, instead of using the default $_/$PSItem.


$Error

This variable is an array representing all the errors that were logged in the session. The errors in this array get added at the beginning of the array, making $Error[0] the last error.

Note: in a try/catch statement, $_ resolves to $Error[0].



$true/$false

Everyone knows $true and $false, so they need no introduction. They're the boolean values in PowerShell.


$LastExitCode

This is the easiest way to retrieve the exit code of the last executable run during the session. It is a number representing, you guessed it, the last exit code for the last run executable.



$MyInvocation

$MyInvocation contains information about the script, function, or script block being run. One such piece of information is the path and name of the script being run. There are other useful pieces of information in there but I'll let you explore those on your own. I will only cover the script path.

Note: there are two other variable that allow you to get the script path $PSScriptRoot and $PSCommandPath.



$Profile

$Profile contains the location of that magical script that gets loaded at the beginning of every PowerShell session. If you do not have a profile script, I highly recommend that you do, it can save you a LOT of time.


Note: The $Profile variable actually contains 4 values:


$PROFILE #Current User, Current Host (Default)
$PROFILE.CurrentUserCurrentHost #Current User, Current Host
$PROFILE.CurrentUserAllHosts #Current User, All Hosts
$PROFILE.AllUsersCurrentHost #All Users, Current Host
$PROFILE.AllUsersAllHosts #All Users, All Hosts


$PSBoundParameters

$PSBoundParameters is a dictionary containing the parameters passed to a script or function. it differs from $args in the fact that it is a dictionary, meaning that you can search for parameters by name. This is particularly useful when you want to check if a function was invoked with a particular parameter.

Note: One advantage of using $PSBoundParameters is the ability to splat the parameters from a script or a function to other scripts, functions, or cmdlets.


function test-function {
    param(
        [string]$test1,
        [string]$test2,
        [string]$test3,
        [int]$test4
    )
    Write-Host "Parameters:" -ForegroundColor Green
    $PSBoundParameters | format-table
    Write-Host "------------------" -ForegroundColor Green
}
test-function -test1 "test1"
test-function -test2 "test2"
test-function -test3 "test3"
test-function -test4 10
test-function -test1 "test1" -test2 "test2" -test3 "test3" -test4 10


$PSCommandPath

OK, you got me, there is an easier way to find the path of the script being run: $PSCommandPath.



$PSScriptRoot

$PSScriptRoot is similar to $PSCommandPath, except it returns the directory in which the script is located.




$PSVersionTable

If you work on multiple systems with varying versions of PowerShell, $PSVersionTable is your friend. It contains information about the version of PowerShell running on the current 



$PWD

If you are familiar with unix systems, you know PWD (which stands for Print Working Directory). This variable does what it says, it prints the current working directory. It is also equivalent to the alias pwd and the cmdlet Get-Location


There you have it, my list of the 11 (well, 13, really) most important PowerShell Automatic variables. What other variables do you think is important to know?


4,467 views

©2019-2020 by Bulles Technologies. Proudly created with Wix.com