The previous section focuses on the definition and use of variables in PowerShell, as well as some tips on how to include special characters in variables or variables at output, as described here.
This section describes built-in variables in PowerShell, or automatic variables.
In the PowerShell command line, you can enter the get-variable command to view all the built-in variables in PowerShell
ps c:\> get-variablename value---- -----$ cls? True^ cls_args {} confirmpreference highconsolefilenamedebugpreference silentlycontinue ...
The following explains the meaning of the built-in variables commonly used in PowerShell.
PowerShell built-in variable name
|
Description |
$$ |
Last token in the last line received in the current session (you can understand that the last command is executed) |
$? |
The execution state of the last operation. This operation can be a PowerShell command, or a return value called EXE, if the last operation succeeds, the $? contains the value when True, otherwise the value contained is false. This built-in is useful when judging whether a previous operation was executed successfully. |
$LastExitCode |
Exit code for the last Windows-based program. Note the difference between the variable and the $? |
$True |
Contains true to use this built-in variable in place of the string "TRUE" in a command or script. |
$False |
Contains false, which can be used in a command or script in place of the string "FALSE". |
$NULL |
Contains null or null values. You can use this variable in commands and scripts to represent NULL instead of using the string "null". If the string is converted to a non-empty string or a non-0 integer, the string can be interpreted as true. |
$_ |
Contains the current object in the pipeline object, which is especially useful when filtering objects in the pipeline or performing the appropriate action commands. The following command is to filter all commands that start with get-com Get-command | Where-object {$_. Name-like "get-com*"} |
$This |
In a script block that defines a script property or Script method, $This variable references the object to be extended. This is the same as this in the high-level language, which represents the object that is currently being referenced. Note that the built-in variable differs from the $_. |
|
|
$PID |
The process identifier (PID) of the process for the current Windows PowerShell session, an integer representation of the number. |
$ShellID |
Identifier of the current shell, such as Microsoft.PowerShell |
$PsUICulture |
The name of the user interface (UI) culture currently used in the operating system (for example, if it is Simplified Chinese, the value is ZH-CN). The UI culture determines which text strings are used for user interface elements, such as menus and messages. This is the value of the system's System.Globalization.CultureInfo.CurrentUICulture.Name property |
$PsCulture |
The name of the culture currently used in the operating system (for example, if it is Simplified Chinese, the value is ZH-CN). The culture determines the display format for items such as numbers, currencies, and dates. This is the value of the system's System.Globalization.CultureInfo.CurrentCulture.Name property. |
|
|
$PsHome |
The full path to the installation directory for Windows PowerShell (typically%windir%\system32\windowspowershell\v1.0). You can use this variable in the path of a Windows PowerShell file. |
$Home |
The full path of the user's home directory, equivalent to the%homedrive% or%homepath% environment variable |
$Pwd |
The full path of the current directory
|
$Host |
Represents the current host application for Windows PowerShell (the popular point is representing the current host). You can use this variable to represent the current host in a command, or to display or change the properties of a host, such as $Host. Version, $Host. CurrentCulture or $host. Ui.rawui.setbackgroundcolor ("Red"). |
$Profile |
The full path to the Windows PowerShell configuration file for the current user and the current host application. You can use this variable to represent a configuration file in a command. |
|
|
$PsVersionTable |
A read-only hash table that represents the details of the currently running PowerShell version, which contains the following items.
Clrversion: Version of the common language runtime (CLR) Buildversion: Build number of the current version Psversion:windows PowerShell Version number Wsmanstackversion:ws-management The version number of the stack Pscompatibleversions: Version of Windows PowerShell compatible with the current version Serializationversion: Version of serialization method Version of the Psremotingprotocolversion:windows PowerShell Remote Management Protocol |
This article from "Flower Blossom Fall" blog, declined reprint!
(9) built-in variables in PowerShell