PowerShell supports constant and read-only variables. We have discussed creating the variables in our previous Article “PowerShell – How to create Variables?“. Through this, we are going to discuss creating read-only and constant variables.
Read-only variables are the variables whose content cannot be modified. Constant variables are the variables whose content also cannot be modified. Then what is the difference between them? We will discuss it through this Article.
Create read-only variables in PowerShell
We already knew that we can create variables using New-Variable
command. We discussed this in our previous article, mentioned above. It allows different options to create the variables. In order to create a read-only variable; you just have to pass ReadOnly
as the value through one of its parameters “-Option”.
PS C:\> New-Variable -Name read_only_var -Value "Hey, you can't change me!" -Option ReadOnly
Now we have the read-only variable assigned with a value. You are NOT allowed to change the values of read-only variables. Hence you MUST assign a value when creating the variable. Later, if you attempt to change it’s value; this command will throw the Error. Here is an example;
PS C:\> $read_only_var = "Wanna give NEW value!"
Cannot overwrite variable read_only_var because it is read-only or constant.
At line:1 char:1
+ $read_only_var = "Wanna give NEW value!"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : WriteError: (read_only_var:String) [], SessionStateUnauthorizedAccessException
+ FullyQualifiedErrorId : VariableNotWritable
Force to change the value of read-only Variable
Doesn’t it possible to change the value of the read-only Variables? Usually, NO. But this command provides an option to do this. We need to instruct to the New-Variable
command to override the value of the read-only variable; by specifying the “-Force” parameter.
“-Force” parameter instructs the command to forcibly change the value of the variable; even though it is a read-only variable.
PS C:\> New-Variable -Name read_only_var -Value "A NEW value to me!" -Option ReadOnly -Force
Create constant variables in PowerShell
This command allows creating constants too. Constants CAN’T be altered once they are created. Hence you must give value to them at the time of defining them. Once defined, you can’t alter the values of the constant variables.
You need to pass Constant
as the value through the “-Option” parameter to create the constant variables. The below example, creates a PI value (mathematical PI); which is constant.
PS C:\> New-Variable -Name pi -Value 3.14159265359 -Option Constant
If you attempt to alter the values of the constant variables, this command will throw the below error.
New-Variable : Cannot overwrite variable pi because it is read-only or constant.
You CANNOT even change the values using “-Force” parameter. This is the difference between read-only and constant variables. The values of read-only variables can be altered through “-Force”; whereas it is not possible with constant variables.
Another interesting thing you need to observe here is; with the “-Force” parameter, you can change the read-only variable to a normal variable. But, with constants; you can’t change them to normal variables.
[..] David
Heya i’m for the first time here. I came across this board and I find It really useful & it helped me out much.
I hope to give something back and help others like you aided me.