PowerShell By Example: Constants

PowerShell supports constants and read-only variables.
Constants are variables that cannot be changed whatever you try.

You create a constant variable with the New-Variable command with the parameter -Option Constant.

New-Variable -Name myConst -Value "This CANNOT be changed" -Option Constant
$myConst

We can’t change te value even with the -Force parameter

New-Variable -Name myConst -Value "I'm going to change it" -Option Constant -Force
New-Variable : Cannot overwrite variable myConst because it is read-only or constant.
At line:1 char:1
+ New-Variable -Name myConst -Value "I'm going to change it" -Option Co ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (myConst:String) [New-Variable], SessionStateUnauthorizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable,Microsoft.PowerShell.Commands.NewVariableCommand