Search notes:

PowerShell: Variables

A variable stores (and names) an object of a specific type. The variable itself, however, is not typed, although it can be constrained to hold a specific type.
The currently assigned type of a variable can be determined with getType():
$x = 42
$x.getType()
A variable to which no value was assigned has the value $null.

Variable names

As in a Unix shell or in Perl, variable names are prefixed with a dollar sign ($) when referenced in a script or on the command line.
The names of variables are not case sensitive.
A variable name can even start with a digit:
$42 = 'forty-two'
write-host "$42"

Categories of variables

Variables can be categorized like so:

Constraining variable types

A variable can be declared to be strongly or loosly typed.
The following variable declaration creates a strongly typed variable:
[int] $theNum = 42
See also constrained types for function parameters.
Variables in the env:, alias: and the file system namespaces are constrained to type string.
Variables in the function: namespace are constrained to the type scriptblock.

Constants

PS C:\> set-variable num -option constant -value  42
PS C:\> set-variable txt -option readOnly -value 'Hello world'

PS C:\> $num = 17
Cannot overwrite variable num because it is read-only or constant…

PS C:\> $txt ='foo'
Cannot overwrite variable txt because it is read-only or constant…

PS C:\> remove-variable txt -force

PS C:\> remove-variable num -force
remove-variable : Cannot remove variable num because it is constant or read-only…
$V = new-object System.Management.Automation.PSVariable -argumentList @(
        'tq84'       ,  # Variable name
        'foo bar baz',  # Value
        [System.Management.Automation.ScopedItemOptions]::Constant
     )

$executionContext.SessionState.PSVariable.Set($V)

write-host "tq84 = $tq84"

Listing variables

The names and values of variables are found in the drive variable:. Thus, they can be displayed with
get-childItem variable:

Checking if a variable has been assigned a value

The test-path cmdLet allows to check if a variable has been assigned a value:
PS C:\> test-path variable:/x
False
PS C:\> $x = 42
PS C:\> test-path variable:/x
True

Assign user-input to a variable

The cmdLet that allows to prompt a user for text and then assign the text to a varible is read-host.

.NET classes related to variables

Some .NET classes that pertain to variables are:
Such a list can be produced with
get-childItem variable: | forEach-object {$_.GetType().fullName} | sort-object -unique

See also

automatic and preference variables
The preference variable $maximumVariableCount controls the maximum number of variables that PowerShell keeps track of.
A PowerShell script can be made more robust by making sure referenced variables actually exist by using the statement set-strictMode -version 1 in a script.
A variable is an item.
The cmdLet noun variable
The .NET type of PowerShell variables is System.Management.Automation.PSVariable.
Because a variable is just a special item type, the value of a variable can be queried with get-content.
Variables exist in a scope.
Access module-private variables with the call operator (&).
Create a global variable in a function.

Index