Converting VBScript's Option Explicit Statement

Definition: Forces explicit declaration of all variables in a script.

Option Explicit

In VBScript, the Option Explicit statement forces you to declare all variables before using them in a script. To do the same thing in Windows PowerShell use the Set-PSDebug Cmdlet and the -strict parameter, like so:

set-psdebug -strict

What will that do for you? Well, suppose you now try to execute a command similar to this, using the uninitialized variable $z:

$a = 2 + $z

You won’t get an answer; instead you’ll get the following error message:

The variable $z cannot be retrieved because it has not been set yet.

To turn off this behavior, use this command:

set-psdebug -off

Return to the VBScript to Windows PowerShell home page