Chapter 4. PowerShell Automatic Variables

PowerShell defines and populates several variables automatically. These variables let you access information about the execution environment, PowerShell preferences, and more.

Table 4-1 provides a listing of these automatic variables and their meanings.

Table 4-1. Windows PowerShell automatic variables: Variables automatically used and set by Windows PowerShell

Variable

Meaning

$$

Last token of the last line received by the shell.

$?

Success/fail status of the last operation.

$^

First token of the last line received by the shell.

$_

Current pipeline object in a pipelined script block.

$args

Array of parameters passed to the script, function, or script block.

$confirmPreference

Preference that controls the level of impact that operations may have before requesting confirmation. Supports the values none, low, medium, high. A value of none disables confirmation messages.

$consoleFilename

Filename of the PowerShell console file that configured this session, if one was used.

$currentlyExecutingCommand

Currently executing command, when in a suspended prompt.

$debugPreference

Preference that controls how PowerShell should handle debug output written by a script or cmdlet. Supports the values SilentlyContinue, Continue, Inquire, and Stop.

$error

Array that holds the terminating and nonterminating errors generated in the shell.

$errorActionPreference

Preference that controls how PowerShell should handle error output written by a script or cmdlet. Supports the values SilentlyContinue, Continue, Inquire, and Stop.

$errorView

Preference that controls how PowerShell should output errors in the shell. Supports the values of Normal and CategoryView (a more succinct and categorical view of the error).

$executionContext

Means by which scripts can access the APIs typically used by cmdlets and providers.

$false

Variable that represents the Boolean value False.

$foreach

Enumerator within a foreach loop.

$formatEnumerationLimit

Limit on how deep into an object the formatting and output facilities travel before outputting an object.

$home

User’s home directory.

$host

Means by which scripts can access the APIs and implementation details of the current host and user interface.

$input

Current input pipeline in a pipelined script block.

$lastExitCode

Exit code of the last command. Can be explicitly set by scripts, and is automatically set when calling native executables.

$logEngineHealthEvent

Preference that tells PowerShell to log engine health events, such as errors and exceptions. Supports the values $true and $false.

$logEngineLifecycleEvent

Preference that tells PowerShell to log engine lifecycle events, such as Start and Stop. Supports the values $true and $false.

$logCommandHealthEvent

Preference that tells PowerShell to log command health events, such as errors and exceptions. Supports the values $true and $false.

$logCommandLifecycleEvent

Preference that tells PowerShell to log command lifecycle events, such as Start and Stop. Supports the values $true and $false.

$logProviderHealthEvent

Preference that tells PowerShell to log provider health events, such as errors and exceptions. Supports the values $true and $false.

$logProviderLifecycleEvent

Preference that tells PowerShell to log provider lifecycle events, such as Start and Stop. Supports the values $true and $false.

$matches

Results of the last successful regular expression match (through the –match operator).

$maximumAliasCount

Limit on how many aliases may be defined.

$maximumDriveCount

Limit on how many drives may be defined. Does not include default system drives.

$maximumErrorCount

Limit on how many errors PowerShell retains in the $error collection.

$maximumFunctionCount

Limit on how many functions may be defined.

$maximumHistoryCount

Limit on how many history items are retained.

$maximumVariableCount

Limit on how many variables may be defined.

$myInvocation

Information about the context under which the script, function, or script block was run, including detailed information about the command (MyCommand) and the script that defines it (ScriptName).

$nestedPromptLevel

Nesting level of the current prompt. Incremented by operations that enter a nested prompt (such as $host.EnterNestedPrompt( )) and decremented by the exit statement.

$null

Variable that represents the concept of Null.

$ofs

Output field separator. Placed between elements when PowerShell outputs a list as a string.

$outputEncoding

Character encoding used when sending pipeline data to external processes.

$pid

Process ID of the current PowerShell instance.

$profile

Location and filename of the PowerShell profile for this host.

$progressPreference

Preference that controls how PowerShell should handle progress output written by a script or cmdlet. Supports the values SilentlyContinue, Continue, Inquire, and Stop.

$psHome

Installation location of PowerShell.

$pwd

Current working directory.

$shellId

Shell identifier of this host.

$stackTrace

Detailed stack trace information of the last error.

$this

Reference to the current object in ScriptMethods and ScriptProperties.

$transcript

Filename used by the Start-Transcript cmdlet.

$true

Variable that represents the Boolean value True.

$verboseHelpErrors

Preference that tells PowerShell to output detailed error information when parsing malformed help files. Supports the values $true and $false.

$verbosePreference

Preference that controls how PowerShell should handle verbose output written by a script or cmdlet. Supports the values SilentlyContinue, Continue, Inquire, and Stop.

$warningPreference

Preference that controls how PowerShell should handle warning output written by a script or cmdlet. Supports the values SilentlyContinue, Continue, Inquire, and Stop.

$whatifPreference

Preference that controls how PowerShell should handle confirmation requests called by a script or cmdlet. Supports the values SilentlyContinue, Continue, Inquire, and Stop.

Get Windows PowerShell Pocket Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.