Windows PowerShell, like most other
scripting languages, stores values in variables. Variables are
represented by single-word text strings that begin with the dollar sign
($). Windows PowerShell supports four types of variables: user-created,
automatic, preference, and environment variables.
Working with Variables
The simplest way to create a variable is by using
the assignment operator = to set a variable to a specific value. The
following example creates the variable $string and assigns it the value My String.
PS > $string = "My String"
You can also use the New-Variable cmdlet to
create variables. This cmdlet offers some additional functionality,
such as adding a description, setting the variable to read-only or
constant, and setting a specific scope for a variable. Here is an
example:
PS > New-Variable -Name string -Value "My String" `
>> -Description "Created using New-Variable" -Option ReadOnly -Force
This creates the variable $string and sets the value to My String. We also specify a description, use the Option parameter to set the variable to ReadOnly, and use the -Force switch parameter since the variable already exists.
If we try to assign a new value to the variable
using the assignment operator, an error occurs since the variable is
set to read-only.
PS > $string = "New Value"
Cannot overwrite variable string because it is read-only or constant.
At line:1 char:8
+ $string <<<< = "New Value"
+ CategoryInfo : WriteError: (string:String) [],
SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotWritable
Instead, we can use the Set-Variable cmdlet with the -Force switch parameter to set a new value. We also change the Option parameter from ReadOnly to None.
PS > Set-Variable -Name string -Value "New Value" -Option None -Force
The Get-Variable cmdlet is used to retrieve variables, as follows:
PS > Get-Variable -Name string
Name Value
---- -----
string New Value
This example returns an object of the type System.Management.Automation.PSVariable, which contains properties that describe the variable. If you want to display only the variable’s value, use the -ValueOnly switch parameter.
PS > Get-Variable -Name string -ValueOnly
New Value
This example returns an object of the type System.String. This corresponds to just typing a dollar sign ($) followed by the variable’s name:
You can display additional information about a variable by using the Get-Variable cmdlet and sending the object to the Format-List cmdlet.
PS > Get-Variable -Name string | Format-List
Name : string
Description : Created using New-Variable
Value : New Value
Visibility : Public
Module :
ModuleName :
Options : None
Attributes : {}
To clear the value of a variable, use the Clear-Variable cmdlet.
PS > Clear-Variable -Name string
When using the Clear-Variable cmdlet, the variable’s value is set to null. You can also set a variable to null using the assignment operator.
To delete a variable, use the Remove-Variable cmdlet.
PS > Remove-Variable -Name string
Note
You cannot delete variables set as constants or variables owned by the system.