Converting VBScript's Const Statement

Definition: Declares constants for use in place of literal values.

Const

A constant is nothing more than a variable whose value cannot be changed while a script is running. In Windows PowerShell you can define a constant by using the Set-Variable Cmdlet and specifying three parameters:

  • -name, the name of the constant (don’t include the $).

  • -value, the value of the constant.

  • -optionconstant, to create a constant as opposed to a standard variable.

For example, this command creates a constant named $ForReading, with a value of 1:

set-variable -name ForReading -value 1 -option constant

If you try to assign a new value to this constant you’ll get an error message similar to this:

Cannot overwrite variable ForReading because it is read-only or constant.

Return to the VBScript to Windows PowerShell home page