Set the value of a variable.
Syntax
Set-Variable [-Name] String[] [-value] Object]
[-Include string[]] [-Exclude string[]] [-option option]
[-scope string] [-force] [-passThru] [-whatIf]
[-confirm][-description string] [CommonParameters]
Key
-Name String
The name of the variable(s), may be piped.
-value Object
The value to assign to the variable.
-include string
Items upon which Set-variable will act, excluding all others.
-exclude string
Items upon which Set-variable will not act, include all others.
-option option
The valid options are:
None : Set no options. (default)
ReadOnly: The alias cannot be changed unless you use -Force.
Constant: The alias cannot be changed, even by using -Force.
Private : The alias is available only within the scope specified by -Scope.
It is invisible in all other scopes.
-scope string
The scope in which this alias is valid.
Valid values are "Global", "Local", "Private" or "Script", or a number
relative to the current scope ( 0 through the number of scopes, where
0 is the current scope and 1 is its parent). "Local" is the default.
For more, type "get-help about_scope".
-force
Override restrictions that prevent the command from succeeding, apart
from security settings. Make the best attempt at setting the variable.
-passThru
Pass the object created by this cmdlet through the pipeline.
-whatIf
Describe what would happen if you executed the command without actually
executing the command.
-confirm
Prompt for confirmation before executing the command.
-description string
The description of the variable.
CommonParameters:
-Verbose, -Debug, -ErrorAction, -ErrorVariable, -OutVariable.
Examples
Set the value of the "SS64" variable to "My Sample text ":
PS C:\>set-variable -name SS64 -value "My Sample text"
Create a global, read-only variable that contains all processes on the system:
PS C:\>set-variable -name myprocs -value (Get-Process) -option constant -scope global
In the example above, the value is enclosed in parentheses this executes the command: (Get-Process) before storing the result in the variable, rather than just storing the text "Get-Process".
“O, swear not by the moon, the fickle moon, the inconstant moon, that monthly changes in her circle orb, Lest that thy love prove likewise variable.” - Shakespeare
Related:
Clear-Variable - Remove the value from a variable
Get-Variable - Get a powershell variable
New-Variable - Create a new variable
Remove-Variable - Remove a variable and its value
Equivalent bash command: env - Display, set, or remove environment variables