PowerShell By Example: Read-Only Variables

PowerShell supports constants and read-only variables.
Read-only variables are variables that cannot be changed, in a regular way.

You create a read-only variable with the New-Variable command with the parameter -Option ReadOnly.

New-Variable -Name myVar -Value 1337 -Option ReadOnly

Let’s try and change it

$myVar = 31337
Cannot overwrite variable myVar because it is read-only or constant.
At line:1 char:1
+ $myVar = 31337
+ ~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (myVar:String) [], SessionStateUnauthorizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable

To change the variable we need to use the -Force parameter:

$myvar          # Should output 1337
New-Variable -Name myVar -Value 31337 -Option ReadOnly -Force
$myVar          # Should output 31337

In this case the value can be changed