When you create a variable you usually want to be able to change its value – the clue is in the term variable. Sometimes though you might want to use a value that doesn’t change – you want read only and constant variables.
Standard variables are changeable:
PS> $x = 10
PS> $x
10
PS> $x += 5
PS> $x
15
PS> $x -= 3
PS> $x
12
The variable holds a value that can be changed to match your needs. Another example is the variable used in a for loop:
PS> for ($i=1; $i -lt 6; $i++){$i}
1
2
3
4
5
If you want to use a value in your code that is pre-set and doesn’t change you have a couple of options – read only variables and constants.
To create a read only variable use New-variable with the ReadOnly option
PS> New-Variable -Name x -Value 10 -Option ReadOnly
PS> $x
10
Now if you try to change the value
PS> $x += 5
Cannot overwrite variable x because it is read-only or constant.
At line:1 char:1
+ $x += 5
+ ~~~~~~~
+ CategoryInfo : WriteError: (x:String) [], SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotWritable
You can also change an existing variable to be read only
PS> $y = 20
PS> Set-Variable -Name y -Option ReadOnly
PS> $y
20
PS> $y -= 5
Cannot overwrite variable y because it is read-only or constant.
At line:1 char:1
+ $y -= 5
+ ~~~~~~~
+ CategoryInfo : WriteError: (y:String) [], SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotWritable
A read only variable is protected from deletion
PS> Remove-Variable -Name x
Remove-Variable : Cannot remove variable x because it is constant or read-only. If the variable is read-only, try the
operation again specifying the Force option.
At line:1 char:1
+ Remove-Variable -Name x
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (x:String) [Remove-Variable], SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand
Time to be a PowerShell Jedi and use the force:
PS> Remove-Variable -Name x -Force
the variable is removed.
But what if you want to prevent the variable from being removed. Then you have to make it a constant:
PS> New-Variable -Name x -Value 10 -Option Constant
PS> $x
10
PS> $x += 5
Cannot overwrite variable x because it is read-only or constant.
At line:1 char:1
+ $x += 5
+ ~~~~~~~
+ CategoryInfo : WriteError: (x:String) [], SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotWritable
PS> Remove-Variable -Name x
Remove-Variable : Cannot remove variable x because it is constant or read-only. If the variable is read-only, try the
operation again specifying the Force option.
At line:1 char:1
+ Remove-Variable -Name x
+ ~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (x:String) [Remove-Variable], SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand
PS> Remove-Variable -Name x -Force
Remove-Variable : Cannot remove variable x because it is constant or read-only. If the variable is read-only, try the
operation again specifying the Force option.
At line:1 char:1
+ Remove-Variable -Name x -Force
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (x:String) [Remove-Variable], SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotRemovable,Microsoft.PowerShell.Commands.RemoveVariableCommand
You can’t modify the variable’s value and you can’t delete it.
The variable will disappear when you close the PowerShell console.