## My try to create a global constant
Set-Variable -Name c -Value "x" -Option Constant -Scope Global -Force
Write-Host $c ## -> x
$c = "y" ## -> WriteError: (C:String) [], SessionStateUnauthorizedAccessException
## -> VariableNotWritable
Write-Host $c ## -> x
function test {
Write-Host $c ## -> x
$c = "xxxx"
Write-Host $c ## -> xxxx
}
test
My variable $c is global accessible, but not constant in all cases. Try to change the value inside the function test() and PowerShell allows a change of value.
Is there a way to create a true global constant variable?
Background:
I have a main script. The main script loads a few modules. Through all the modules and in the main script I need some fixed file and registry paths. So I want to declare these paths as global constants.