How to force a warning or error when using an undefined variable?

In a powershell script, if I try to use an undefined variable, it continues on its way without indicating any warning or error that I made.

For example, the following script

echo "a"
echo $nonexistant_variable
echo "c"

gives way

a
c

Is there a way to get powerShell to tell me that the variable I'm trying to use is undefined?

+4
source share
2 answers

You can use strict mode:

Set-strictmode -version latest

This will give you a warning and errors if you make common mistakes (for example, using an undeclared variable, etc.).

You can also use PSDebug -Strict

Set-PSDebug -Strict
+4
source

Set-PSDebug -Strict :

-strict

, , ,

#I belong in your PowerShell (ISE)-profile
Set-PSDebug -Strict
+2

All Articles