PowerShell, like many programming and scripting languages, includes a set of special variables that are reserved for specific purposes. These variables often start with a dollar sign ($) and provide access to various functionalities and information. This article will explore some of the key special variables in PowerShell and demonstrate their use with examples.
You can also visit our previous article to make understanding on PowerShell variables.
1. $_
The `$_` variable is an automatic variable that represents the current object in the pipeline. It is commonly used in cmdlets that perform an action on each object in the pipeline.
Example:
Suppose we have a list of numbers and we want to square each of them:
1..5 | ForEach-Object { $_ * $_ }
The output will be:
Output:1 4 9 16 25
2. $PSItem
This is an alias for `$_`. It represents the current item in the pipeline and can be used interchangeably with `$_`.
3. $?
The `$?` variable indicates the success or failure of the last command. It returns a boolean value, where `$true` means the last command was successful, and `$false` means it failed.
Example:
Get-ChildItem nonExistentFolder
echo $?
If the folder does not exist, the output will be `$false`.
4. $^
The `$^` is a special automatic variable. It contains the first token of the last line/input that was sent to the shell. It can be useful for debugging or interactive sessions.
Here’s a simple demonstration:
PS> 10 + 20
30
PS> $^
10
After executing the 10 + 20 command, `$^` will contain the first token of that command, which is 10.
Remember, it holds the first “token”, which could be a command, a parameter, a variable, etc. Depending on the context, it can be particularly useful for automation scripts where you might want to recall or act upon the initial segment of the previous command.
5. $PSVersionTable
This variable provides details about the version of PowerShell being used. It’s a hashtable containing various properties like PSVersion, BuildVersion, CLRVersion, etc.
Example:
$PSVersionTable.PSVersion
This command will return the version of PowerShell you’re currently using.
6. $PWD
`$PWD` gives the current directory (Present Working Directory) in which the script or command is being run.
Example:
echo $PWD
This will print the current directory.
7. $HOME
The $HOME variable points to the home directory of the current user.
Example:
echo $HOME
This will display the home directory path of the current user.
8. $args
This variable represents an array of undeclared parameters or arguments passed to a function, script, or script block.
Example:
function ShowArgs {
$args
}
ShowArgs a b c
The output will be:
Output:a b c
9. $Error
This variable contains an array of error objects representing the most recent errors. $Error[0] contains the most recent error, and $Error[1] contains the second most recent error, and so on.
Example:
Get-ChildItem nonExistentFolder
$Error[0].Exception.Message
The above command will display the error message generated when trying to get items from a non-existent folder.
10. $LASTEXITCODE
This variable returns the exit code of the last external application or script that was run. It is particularly useful when you’re running non-PowerShell commands or scripts and need to check their exit status.
Example:
If you run a command like:
ping localhost
echo $LASTEXITCODE
If ping runs successfully, the exit code $LASTEXITCODE will typically be 0.
Conclusion
PowerShell’s special variables offer a wide array of functionalities that can be leveraged in scripting and interactive sessions to gain insights, handle errors, or manipulate data. Familiarizing oneself with these can significantly enhance productivity and streamline operations in PowerShell.