Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create global constant variables in PowerShell

## 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.

like image 277
joop s Avatar asked Nov 04 '17 23:11

joop s


People also ask

How to set the value of a global variable in PowerShell?

We can also pass a global variable as a parameter to a function and set its value inside the function. But of course, you’ll have to give the global variable as a REF object. The simplest way to set the values to a Windows PowerShell global variable is to use the Set-Variable cmdlets. The script will display the value of 6.

How do I set a variable in PowerShell to all processes?

PowerShell Set-Variable -Name "processes" -Value (Get-Process) -Option Constant -Scope global -Description "All processes" -PassThru | Format-List -Property * The command uses the Set-Variable cmdlet to create the variable.

Does PowerShell support constant and read-only variables?

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.

How to override a variable in PowerShell?

If the variable has values previously, then the value will be overridden. Set-Variable cmdlet is used to create the variable in PowerShell, as well as assigning value to the variable. Below is an example, where we are setting value to the variable and also getting the value from the variable.


Video Answer


2 Answers

The global variable $c remains constant, but with the assignment $c = "xxxx" another local variable $c is defined that takes the new value and masks the global variable in the local context.

Demonstration:

PS C:\> Set-Variable -Name c -Value "x" -Option Constant -Scope Global -Force
PS C:\> function test {
>>     Get-Variable -Name c -Scope Global
>>     Get-Variable -Name c -Scope Local
>>     $c = "xxxx"
>>     Get-Variable -Name c -Scope Global
>>     Get-Variable -Name c -Scope Local
>> }
>>
PS C:\> test

Name                           Value
----                           -----
c                              x
Get-Variable : Cannot find a variable with the name 'c'.
At line:3 char:5
+     Get-Variable -Name c -Scope Local
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (c:String) [Get-Variable], ItemNotFoundException
    + FullyQualifiedErrorId : VariableNotFound,Microsoft.PowerShell.Commands.GetVariableCommand

c                              x
c                              xxxx

The first Get-Variable -Name c -Scope Local call fails, because there is no local variable $c defined yet.

The issue can be avoided by prefixing the variable/constant with the correct scope:

PS C:\> Set-Variable -Name c -Value "x" -Option Constant -Scope Global -Force
PS C:\> function test {
>>     $global:c
>>     $global:c = "xxxx"
>>     $global:c
>> }
>>
PS C:\> test
x
Cannot overwrite variable c because it is read-only or constant.
At line:3 char:5
+     $global:c = "xxxx"
+     ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (c:String) [], SessionStateUnauthorizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable

x

or by defining the constant for all scopes:

PS C:\> Set-Variable -Name c -Value "x" -Option Constant, AllScope -Force
PS C:\> function test {
>>     $c
>>     $c = "xxxx"
>>     $c
>> }
>>
PS C:\> test
x
Cannot overwrite variable c because it is read-only or constant.
At line:3 char:5
+     $c = "xxxx"
+     ~~~~~~~~~~~
    + CategoryInfo          : WriteError: (c:String) [], SessionStateUnauthorizedAccessException
    + FullyQualifiedErrorId : VariableNotWritable

x
like image 68
Ansgar Wiechers Avatar answered Oct 17 '22 16:10

Ansgar Wiechers


For a complete answer:

#________________________________________________
# Example1, without prefix
#________________________________________________
$c="val1"

# Print output c variable
$c

function test
{
    $c="val2"
}

test

# Print output c variable (c not change)
$c

#________________________________________________

# Example2, with prefix global
#________________________________________________
$global:c="val1"

# Print output c variable
$global:c

function test2
{
    $global:c="val2"
}

test2

# Print output c variable (c change)
$global:c

#________________________________________________

# Example3, with prefix script
#________________________________________________
$script:c="val1"

# Print output c variable
$script:c

function test3
{
    $script:c="val2"
}

test3

# Print output c variable (c change)
$script:c

#________________________________________________

# Example 4, with get and set variable --> see answer of Ansgar Wiechers
#________________________________________________

NB: Difference of global and script is a scope question. For more details, see here.

like image 43
Esperento57 Avatar answered Oct 17 '22 15:10

Esperento57