PowerTip: Two Quick Ways to Check Variables in PowerShell

PowerTip: Two Quick Ways to Check Variables in PowerShell

Rate This
  • Comments 2

Summary: Check for undeclared variables.

Hey, Scripting Guy! Question How can I cause the script to generate an error if a variable has not been declared?

Hey, Scripting Guy! Answer Here are a couple of ways to do this with Windows PowerShell.

a. Place set-psdebug –strict anywhere in the script. Any nondeclared variable will generate an error when accessed.

b. Use Set-StrictMode –Version latest

 

Leave a Comment
  • Please add 4 and 6 and type the answer here:
  • Post
  • So it doesn't automatically tell you?

  • @Livio Von Buren, no by default PowerShell does not tell you if you have an undeclared variable. Use Strict mode to do this. If you want this notification by default, then add the command to your Powershell profile.

Page 1 of 1 (2 items)