Does PowerShell support constants?
PowershellConstantsPowershell Problem Overview
I would like to declare some integer constants in PowerShell.
Is there any good way to do that?
Powershell Solutions
Solution 1 - Powershell
Use
Set-Variable test -Option Constant -Value 100
or
Set-Variable test -Option ReadOnly -Value 100
The difference between "Constant" and "ReadOnly" is that a read-only variable can be removed (and then re-created) via
Remove-Variable test -Force
whereas a constant variable can't be removed (even with -Force).
See this TechNet article for more details.
Solution 2 - Powershell
Here is a solution for defining a constant like this:
const myConst = 42
Solution taken from http://poshcode.org/4063
function Set-Constant {
<#
.SYNOPSIS
Creates constants.
.DESCRIPTION
This function can help you to create constants so easy as it possible.
It works as keyword 'const' as such as in C#.
.EXAMPLE
PS C:\> Set-Constant a = 10
PS C:\> $a += 13
There is a integer constant declaration, so the second line return
error.
.EXAMPLE
PS C:\> const str = "this is a constant string"
You also can use word 'const' for constant declaration. There is a
string constant named '$str' in this example.
.LINK
Set-Variable
About_Functions_Advanced_Parameters
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=0)]
[string][ValidateNotNullOrEmpty()]$Name,
[Parameter(Mandatory=$true, Position=1)]
[char][ValidateSet("=")]$Link,
[Parameter(Mandatory=$true, Position=2)]
[object][ValidateNotNullOrEmpty()]$Mean,
[Parameter(Mandatory=$false)]
[string]$Surround = "script"
)
Set-Variable -n $name -val $mean -opt Constant -s $surround
}
Set-Alias const Set-Constant
Solution 3 - Powershell
Use -option Constant
with the Set-Variable
cmdlet:
Set-Variable myvar -option Constant -value 100
Now $myvar
has a constant value of 100 and cannot be modified.
Solution 4 - Powershell
To use a specific type of value, say Int64, you can explicitly cast the value used in set-variable.
For instance:
set-variable -name test -value ([int64]100) -option Constant
To check,
$test | gm
And you'll see that it is an Int64 (rather than Int32, which would be normal for the value 100).
Solution 5 - Powershell
I really like the syntactic sugar that rob's answer provides:
const myConst = 42
Unfortunately his solution doesn't work as expected when you define the Set-Constant
function in a module. When called from outside the module, it will create a constant in the module scope, where Set-Constant
is defined, instead of the caller's scope. This makes the constant invisible to the caller.
The following modified function fixes this problem. The solution is based on this answer to the question "Is there any way for a powershell module to get at its caller's scope?".
function Set-Constant {
<#
.SYNOPSIS
Creates constants.
.DESCRIPTION
This function can help you to create constants so easy as it possible.
It works as keyword 'const' as such as in C#.
.EXAMPLE
PS C:\> Set-Constant a = 10
PS C:\> $a += 13
There is a integer constant declaration, so the second line return
error.
.EXAMPLE
PS C:\> const str = "this is a constant string"
You also can use word 'const' for constant declaration. There is a
string constant named '$str' in this example.
.LINK
Set-Variable
About_Functions_Advanced_Parameters
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$true, Position=0)] [string] $Name,
[Parameter(Mandatory=$true, Position=1)] [char] [ValidateSet("=")] $Link,
[Parameter(Mandatory=$true, Position=2)] [object] $Value
)
$var = New-Object System.Management.Automation.PSVariable -ArgumentList @(
$Name, $Value, [System.Management.Automation.ScopedItemOptions]::Constant
)
$PSCmdlet.SessionState.PSVariable.Set( $var )
}
Set-Alias const Set-Constant
Notes:
-
The function only works, when called from outside the module, where it is defined. This is the intended use case, but I would like to add a check, whether it's called from the same module (in which case
Set-Variable -scope 1
should work), when I have found out how to do so. -
I've renamed the parameter
-Mean
to-Value
, for consistency withSet-Variable
. -
The function could be extended to optionally set the
Private
,ReadOnly
andAllScope
flags. Simply add the desired values to the 3rd argument of thePSVariable
constructor, which is called in the above script throughNew-Object
.
Solution 6 - Powershell
PowerShell v5.0 should allow
[static] [int] $variable = 42
[static] [DateTime] $thisday
and the like.