PowerShell CheatSheet – Automatic Variables

Not all, but the most usefull (and used) automatic variables.

Contains the last token in the last line received by the session.

Contains the execution status of the last operation.
It contains TRUE if the last operation succeeded and FALSE if it failed.

Contains the first token in the last line received by the session.

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.

Contains an array of the undeclared parameters and/or parameter values that are passed to a function, script, or script block.
When you create a function, you can declare the parameters by using the param keyword or by adding a comma-separated list of parameters in parentheses after the function name.

Contains an array of error objects that represent the most recent errors.
The most recent error is the first error object in the array ($Error[0]).

Contains an EngineIntrinsics object that represents the execution context of the Windows PowerShell host.
You can use this variable to find the execution objects that are available to cmdlets.

Contains FALSE. You can use this variable to represent FALSE in commands and scripts instead of using the string "false". 
The string can be interpreted as TRUE if it is converted to a non-empty string or to a non-zero integer.

Contains the enumerator of a ForEach-Object loop.
You can use the properties and methods of enumerators on the value of the $ForEach variable.
This variable exists only while the For loop is running. It is deleted when the loop is completed.

Contains the full path of the user's home directory.
This variable is the equivalent of the %homedrive%%homepath% environment variables, typically C:\Users\<UserName>.

Contains an object that represents the current host application for Windows PowerShell.
You can use this variable to represent the current host in commands or to display or change the properties of the host, such as $Host.version or $Host.CurrentCulture, or $host.ui.rawui.setbackgroundcolor("Red").

Contains the exit code of the last Windows-based program that was run.

The $Matches variable works with the -match and -notmatch operators.
When you submit scalar input to the -match or -notmatch operator, and either one detects a match, they return a Boolean value and populate the $Matches automatic variable with a hash table of any string values that were matched.
Contains an object with information about the current command, such as a script, function, or script block.
You can use the information in the object, such as the path and file name of the script ($myinvocation.mycommand.path) or the name of a function ($myinvocation.mycommand.name) to identify the current command.
This is particularly useful for finding the name of the script that is running.

$null is an automatic variable that contains a NULL or empty value.
You can use this variable to represent an absent or undefined value in commands and scripts.

Contains the process identifier (PID) of the process that is hosting the current Windows PowerShell session.

Contains the name of the culture currently in use in the operating system.
The culture determines the display format of items such as numbers, currrency, and dates.
This is the value of the System.Globalization.CultureInfo.CurrentCulture.Name property of the system.
To get the System.Globalization.CultureInfo object for the system, use the Get-Culture cmdlet.

Contains the full path of the installation directory for Windows PowerShell, typically, %windir%\System32\WindowsPowerShell\v1.0.

Contains a path object that represents the full path of the current directory.

Contains a stack trace for the most recent error.

Contains TRUE. You can use this variable to represent TRUE in commands and scripts.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top