Be Careful With Strict-Mode!

by Mar 21, 2011

You will discover that PowerShell is production-oriented. So, if you specify object properties that do not exist, you will simply get back “nothing”:

$Host.LetsSee

This is bad because you will not get a warning if you mistype a property name when developing scripts. As such, script developers should enable strict mode:

Set-StrictMode -Version Latest

Now, when you access a non-existent object property, you will get a warning.
However, Set-StrictMode should only be used on development machines. You should never use it on production machines or in production scripts as PowerShell will not complain about non-existing properties. Have a look:

Dir $env:windir | Where-Object { $_.Length -gt 1MB }

This line is designed to return only files greater that 1MB and it works like a charm. However, if you enable the strict mode, you will get tons of error messages because folders can contain files and sub-folders, and sub-folders do not have a property called length.

 

Twitter This Tip!
ReTweet this Tip!