PowerShell is complete programming or scripting language which provides different types of variables. The Set-Variable
is used to set variable values or create a variable if it does not exist.
Set Variable
The Set-Variable can be used to set or create a variable easily. The -Name
attribute is used to specify the variable name and -Value
attribute is used to set the variable value. In the following example, we set the age variable value which is also created from scratch.
PS> Set-Variable -Name "age" -Value 38
Print Variable Value
The Get-Variable command can be used to print a variable value. The -Name
attribute is used to specify the variable name.
PS> Get-Variable -Name "age"
Set Variable As Public
Variables are created as Private by default. But they can be created as public or private variables can be converted into the public using the -Visibility
attribute In the following example we create the variable as public.
PS> Set-Variable -Visibility Public -Name "age" -Value 38
We can also convert existing variable visibility to the public like below.
PS> Set-Variable -Visibility Public -Name "age"
Set Variable As Private
We can also create or set variables as Private by using the -Visibility
attribute. If the visibility of a variable is Private it is not listed as a variable.
PS> Set-Variable -Visibility Private -Name "age" -Value 38
Or we can convert the existing variable to private.
PS> Set-Variable -Visibility Private -Name "age" -Value 38
Set Variable Scope
The variable scope can be defined by using the -Scope
attribute. The following scopes are provided.
- Global
- Local
- Script
- Private
PS> Set-Variable -Scope Global -Name "age" -Value 38
Set Variable Description
During heavy PowerShell usage, we may create lots of variables. We may need to get some description or hint about previously create variables. The -Description
attribute is used to put some description about the variable.
PS> Set-Variable -Name "age" -Value 38 -Description "My age information"