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.
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 drivevariable:. Thus, they can be displayed with