Tuesday, December 12, 2006

PowerShell can be strict

Normally PowerShell silently ignores the use of uninitialized variables.
For instance, the script below will simply assume that $b has no value, and return the value "5"

$a = 5
$a + $b

It is often useful to force PowerShell to return an error if an unitialized variable is used.
This can be accomplished by enabling "strict" mode:

Set-PSDebug -strict

If the earlier scritp is run again an error is returned:

$a = 5
$a + $b

The variable $b cannot be retrieved because it has not been set yet.
At line:1 char:7
+ $a + $b <<<<


Anonymous said...

Reminds me of "option explicit" in VB 8-)

Jakob Bindslet said...

exactly !