Variables can have an optional description that helps you keep track of what the variable was intended for. However, this description appears to be invisible:
# Create variable with description:
New-Variable myvariable -value 100 -description "test variable" -force # Variable returns only the value:
$myvariable
100
# Dir and Get-Variable also do not deliver the description:
Dir variable:\myvariable Name Value ---- --- myvariable 100 Get-Variable myvariable Name Value ---- --- myvariable 100
By default, PowerShell only shows the most important properties of an object, and the description of a variable isn't one of them. If you'd like to see the description, you have to explicitly request it. You can do this by using the cmdlet Format-Table (you'll learn much about this in Chapter 5). Using
Format-Table, you can specify the properties of the object that you want to see:
# variable contains a description:
dir variable:\myvariable |
Format-Table Name, Value, Description -autosize
Name Value Description ---- --- ---
"Automatic" PowerShell Variables
PowerShell uses variables, too, for internal purposes and calls those "automatic variables." These variables are available right after you start PowerShell since PowerShell has defined them during launch. The drive variable: provides you with an overview of all variables:
Dir variable:
Name Value ---- --- Error {}
DebugPreference SilentlyContinue
PROFILE C:\Users\Tobias Weltner\Documents\ WindowsPowerShell\Micro...
HOME C:\Users\Tobias Weltner (...)
To understand the meaning of automatic variables, you can simply view their description:
Dir variable: | Sort-Object Name |
Format-Table Name, Description -autosize -wrap
Name Description ---- --- $
? Execution status of last command. ^
_
ConfirmPreference Dictates when confirmation should be requested. Confirmation is requested when the ConfirmImpact of the operation is equal to or greater than $ConfirmPreference. If $ConfirmPreference is None, actions will only be confirmed when Confirm is specified.
ConsoleFileName Name of the current console file.
DebugPreference Dictates action taken when an Debug message is delivered.
Error
ErrorAction Dictates action taken when an Error message is Preference delivered.
ErrorView Dictates the view mode to use when displaying errors.
ExecutionContext The execution objects available to cmdlets. false Boolean False
FormatEnumeration Dictates the limit of enumeration on formatting Limit IEnumerable objects.
HOME Folder containing the current user's profile. Host This is a reference to the host of this Runspace.
MaximumAliasCount The maximum number of aliases allowed in a session.
MaximumDriveCount The maximum number of drives allowed in a session.
MaximumErrorCount The maximum number of errors to retain in a session.
MaximumFunctionCount The maximum number of functions allowed in a session.
MaximumHistoryCount The maximum number of history objects to retain in a session.
MaximumVariableCount The maximum number of variables allowed in a session.
MyInvocation
NestedPromptLevel Dictates what type of prompt should be displayed for the current nesting level.
null References to the null variable always return the null value. Assignments have no effect. OutputEncoding The text encoding used when piping text to a native executable.
PID Current process ID. PROFILE
ProgressPreference Dictates action taken when Progress Records are delivered.
PSHOME Parent folder of the host application of this Runspace.
PWD
ReportErrorShow Causes errors to be displayed with a description ExceptionClass of the error class.
ReportErrorShow Causes errors to be displayed with the inner InnerException exceptions.
ReportErrorShow Causes errors to be displayed with the source of Source the error.
ReportErrorShow Causes errors to be displayed with a stack trace.
StackTrace
ShellId The ShellID identifies the current shell. This is used by #Requires.
StackTrace
true Boolean True
VerbosePreference Dictates the action taken when a Verbose message is delivered.
WarningPreference Dictates the action taken when a Warning message is delivered.
WhatIfPreference If true, WhatIf is considered to be enabled for all commands.
Most automatic variables are very well documented. Variables are assigned to three categories:
• User information: PowerShell permanently stores some important information. For
example, the path name of the standard profile in $HOME. In addition, some standard variables, like $true and $false, are set.
• Fine adjustments: Numerous default settings allow the behavior of PowerShell to be
modified and customized. For example, you can set how detailed error messages are
• Running time information: PowerShell returns valuable information when it executes
statements. For example, a function can determine who calls it, or a script can determine the location of its folder.
In other respects, automatic variables are no different from the variables you define yourself as you can read the contents and use them in much the same way:
# Verify user profile: $HOME
C:\Users\UserA
# Verify PowerShell Process -id and access profile:
"current process -ID of PowerShell is $PID"
current process -ID of PowerShell is 6116
Get-Process -id $PID
Handles NPM(K) PM(K) WS(K) VM(M) CPU(s) Id ProcessName --- --- --- --- --- --- -- --- 656 22 107620 72344 334 118,69 6116 PowerShell
# Open the standard user profile in notepad for editing:
notepad $profile
To find out more, use Get-Help:
Get-Help about_Automatic_variables
PowerShell write protects several of its automatic variables. While you can read them, you can't modify them. That makes sense because information, like the process-ID of the PowerShell console or the root directory, should not be modified.
$pid = 12
Cannot overwrite variable "PID" because it is read-only or constant.
At line:1 char:5 + $pid <<<< = 12
A little later in this chapter, you'll find out more about how write-protection works. You'll then be able to turn write-protection off and on for variables that already exist. However, you should never do this for automatic variables because that can cause the PowerShell console to crash. One reason is because
PowerShell continually modifies some variables. If you set them to read-only, PowerShell may stop and not respond to any inputs.