While PowerShell does have Set-PSDebug and Set-StrictMode, fundamentally it is a bad language and is not parsing scripts properly before executing them. It will gladly ignore code that is invalid that it has not run. There is no way to validate Powershell code without executing it.
Set-PSDebug -strict
Set-StrictMode -version latest
$ErrorActionPreference = 'Stop'
if($false)
{
Write-Output $fnord #undeclared variable
}
#powershell should halt because of an error and not get here.
write-output "this language is clownshoes"
yields
this language is clownshoes
Contrast this to parse errors, which behave reasonably:
Set-PSDebug -strict
Set-StrictMode -version latest
$ErrorActionPreference = 'Stop'
if($false)
{
() #parseerror
}
#powershell should halt because of an error and not get here.
write-output "this text will not show"
yields
At line:9 char:4
+ () #parseerror
+ ~
An expression was expected after '('.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ExpectedExpression
So, in short, while you can have powershell error out when it attempts to read from an undeclared variable, there is no way to have powershell properly check your script for undeclared variables at parse-time.
Set-StrictMode