Author : MD TAREQ HASSAN | Updated : 2023/03/17

Constant syntax

PowerShell does not have keyword for constant. Use “Set-Variable” or “New-Variable” cmdlet with “-Option Constant”.

# Set-Variable
Set-Variable -Name PI -Value 3.1416 -Option Constant
Write-Host $PI

$PI = 3.3 # error: Cannot overwrite variable PI because it is read-only or constant.

# New-Variable
New-Variable -Name PI -Value 3.1416 -Option Constant
Write-Host $PI

Readonly

Set-Variable -Name RO_V -Value 25 -Option ReadOnly
Write-Host $RO_V # 25

$RO_V = 12 # error: Cannot overwrite variable RO_V because it is read-only or constant.