Learn about Windows PowerShell
Summary: Learn how to force Windows PowerShell to comply with basic best practices.
Is there an easy way for me to help Windows PowerShell comply with basic best practices?
Use the Set-StrictMode –Version Latest command to ensure that you cannot reference things such as uninitialized variables and non-existent properties of an object:
Set-StrictMode -Version latest
The previous post "PowerShell Best Practices for the Console" mentions "I do not use Set-StrictMode". It would be useful to clarify when this mode is useful/recommended and when it is not.
Sorry for a new comment on an old post, but I agree with Roman. Is there or could there be a post on why and when to use this? Pitfalls of not using it?
I agree for newbees and learners. In the beginning you want to learn good code layout and get adept at basics. You should try to use Set-StrictMode to learn how it affects your code but leave it out until you are ready to publish scripts. At that point
you would want to do everything possible to make distributed code reliable and predictable. This can also help keep others from modifying code badly although they can just remove the line. Novices would not normally understand this.
Be careful to not confuse this with "#requires -version n.n"
In my opinion it is very useful for finding crazy mistakes and typos once you are familiar with its behavior. It will not compensate for everything and can be a pain in a multi-version environment. I suggest testing it extensively before widely deploying it
as a standard but working towards that in an enterprise environment is preferred.