Automatic Variables
The Windows PowerShell automatic variables are fixed variables that store state information. Table 2 describes these variables.
Table 2. Windows PowerShell Automatic Variables
| Variable | Contents |
|---|
| $$ | The last token in the last line received by the session |
| $? | True or false, depending on the last performed operation |
| $^ | The first token from the last line received by the session |
| $_ | The current object in the pipeline object |
| $args | An array of values passed to a function or a script |
| $ConsoleFileName | The path of the console file that was most recently used in the session |
| $Error | An array of error objects representing the most recent error that occurred |
| $Event | A PSEventArgs object that represents the event that is being processed |
| $EventSubscriber | A PSEventSubscriber object that represents the event subscriber of the event that is being processed |
| $ExecutionContext | An EngineIntrinsics object that represents the execution context of the Windows PowerShell host |
| $false | Boolean false |
| $foreach | The enumerator of a foreach loop |
| $Home | User’s home directory |
| $Host | Information regarding the current host |
| $input | An enumerator that contains the input passed to a function |
| $LastExitCode | The last exit code of the last program that was run |
| $Matches | Result of the last successful regular expression match |
| $MyInvocation | Information regarding the context under which the script, function, or script block was run |
| $NestedPromptLevel | The current prompt level |
| $NULL | An empty value |
| $PID | The process identifier of the process that is hosting the current Windows PowerShell session |
| $Profile | The full path to the Windows PowerShell profile for the current user |
| $PSBoundParameters | A dictionary of the active parameters and their current values |
| $PsCmdlet | The cmdlet that is being run |
| $PsCulture | The name of the culture currently in use |
| $PsDebugContext | Information about the debugging environment while debugging |
| $PsHome | The full path of the installation directory of Windows PowerShell |
| $PsScriptRoot | The directory from which the script module is being executed |
| $PsUICulture | The name of the user interface culture that is currently in use in the operating system |
| $PsVersionTable | A read-only hashtable containing details about the version of Windows PowerShell |
| $Pwd | The current directory |
| $ShellID | Identifier of the current shell |
| $SourceArgs | Objects that represent the event arguments of the current event |
| $SourceEventArgs | The first object that represents the first event argument that derives from EventArgs of the event that is being processed |
| $This | Reference to the current object in script methods and properties |
| $true | Boolean true |
You can list the variables in Windows PowerShell by using the Get-ChildItem or Get-Variable cmdlet.
PS > Get-ChildItem Variable:
PS> Get-Variable
These commands return both automatic variables and user-created variables.
To display the value of a specific automatic variable, simply type the automatic variable’s name.
PS > $PSHOME
C:\Windows\System32\WindowsPowerShell\v1.0
It is also possible to use the value of a variable
as input to cmdlets in Windows PowerShell. In the next example, we use
the value of the $PsHome variable with the Get-ChildItem cmdlet to retrieve all items from the Windows PowerShell home directory.
PS > Get-ChildItem $PSHOME
When working with pipelines and loops in Windows
PowerShell, you use the automatic variable $_ to handle the current
object in the pipeline.
You can get more information about automatic variables by using the Get-Help cmdlet.
PS > Get-Help about_Automatic_variables
Preference Variables
Windows PowerShell includes a set of preference
variables that let you customize its behavior. The preference variables
affect the environment and how commands behave. Table 3 describes the preference variables available in Windows PowerShell.
Table 3. Windows PowerShell Preference Variables
| Variable | Description |
| $ConfirmPreference | Sets the level of impact that operations have before requesting confirmation: none, low, medium, or high |
| $DebugPreference | Controls how Windows PowerShell handles debugging messages written by a script or a cmdlet |
| $ErrorActionPreference | Sets the default error-handling action |
| $ErrorView | Controls how Windows PowerShell should output errors to the shell |
| $FormatEnumerationLimit | Determines how many enumerated items are included in a display |
| $LogCommandHealthEvent | Logs errors and exceptions in command |
| | initialization and processing |
| $LogCommandLifeCycleEvent | Logs the start and stop of a command in a pipeline |
| $LogEngineHealthEvent | Logs session error and failures |
| $LogEngineLifeCycleEvent | Logs the opening and closing of a session |
| $LogProviderLifeCycleEvent | Logs adding and removing of providers in Windows PowerShell |
| $LogProviderHealthEvent | Logs provider errors |
| $MaximumAliasCount | Determines how many aliases are permitted in the current session |
| $MaximumErrorCount | Determines how many errors are saved in the error history |
| $MaximumFunctionCount | Determines how many functions are permitted in the current session |
| $MaximumHistoryCount | Determines how many commands are saved in the command history for the current session |
| $MaximumVariableCount | Determines how many variables are permitted in the current session |
| $OFS | Output
field separator—specifies the character that an element in an array is
separated with when the array is converted to a stringj |
| $OutputEncoding | Determines the default character encoding used by Windows PowerShell |
| $ProgressPreference | Determines how Windows PowerShell responds to progress updates generated by a script |
| $PSEmailServer | Specifies the default e-mail server used by Windows PowerShell cmdlets |
| $PSSessionApplicationName | Specifies the default application name for a remote command that uses WS-Management technology |
| $PSSessionConfigurationName | Specifies the default session configuration that is used for PSSessions created in the current session |
| $PSSessionOption | Establishes the default values for advanced user options in a remote session |
| $VerbosePreference | Controls how Windows PowerShell handles verbose messages written by a script or a cmdlet |
| $WarningPreference | Controls how Windows PowerShell handles warning messages written by a script or a cmdlet |
| $WhatIfPreference | Determines if WhatIf is automatically enabled for every command that supports it |
You can modify the preference variables by changing
their value. For example, the following example changes the warning
preference to silently continue.
PS > $WarningPreference = "SilentlyContinue"
PS > $WarningPreference
SilentlyContinue
When you modify a preference variable in Windows
PowerShell, the change affects only the current session. In order to
make a persistent change to a preference variable, add the modification
to a profile script, as this example shows:
'$WarningPreference = "SilentlyContinue"' | Out-File $PSHOME\profile.ps1 -Append
Environment Variables
Environment variables store information regarding
the operating system environment. You can display the environment
variables available using the Get-ChildItem cmdlet.
Here’s how to retrieve a specific environment variable:
PS > Get-ChildItem env:COMPUTERNAME
Name Value
---- -----
COMPUTERNAME SPSERVER01
To display the value of the environment variable, add its name.
PS > $env:COMPUTERNAME
SPSERVER01
As with other variables, you can modify an environment variable by changing its value, as in this example:
PS > $env:COMPUTERNAME = "NewName"
PS > $env:COMPUTERNAME
NewName
As with preference variables, all changes made to
the environment variables using Windows PowerShell affect only the
current session. To permanently change environment variables, add the
modifications to a profile script.
Note
You can also permanently change environment variables through the SetEnvironmentVariable() static method available from the System.Environment class.