{"id":6141,"date":"2008-05-21T08:47:00","date_gmt":"2008-05-21T08:47:00","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2008\/05\/21\/strict-mode-v1-debug-tool-v1-v2-best-practice\/"},"modified":"2019-02-18T13:15:55","modified_gmt":"2019-02-18T20:15:55","slug":"strict-mode-v1-debug-tool-v1-v2-best-practice","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/strict-mode-v1-debug-tool-v1-v2-best-practice\/","title":{"rendered":"Strict Mode:  V1 Debug Tool V1 \/ V2 Best Practice"},"content":{"rendered":"<p>When we designed PowerShell we decided that we wanted to&nbsp;have Shell&nbsp;syntax and semantics because this is what was missing from the market and because it supports the ad hoc development practice that is so suited to the needs of administrators.&nbsp; We also observed that most shells ran out of power pretty quickly and people had to throw away their scripts and start from scratch with a more powerfull scripting language like Perl.&nbsp; Sadly, sometimes those environments ran out of power as well and people had to throw them away and start from scratch using a systems programming language like C++.&nbsp; We looked at those things and decided that PowerShell ALSO had to be a great scripting language and be decent systems programming language.&nbsp; This is the orgin of our quest to have the widest possible dynamic range.<\/p>\n<p>You don&#8217;t need to think about this very long to realize that we were going to run into some problems.&nbsp; Boy did we!&nbsp; That said, I was surprised at how many would disappear if you resisted the urge to say, &#8220;choose between Shell and Programming&#8221;.&nbsp; If you worked hard, got creative, and struggled with it, most of these apparent conflicts would give way to a great solution.&nbsp; That only worked so far and there were times when we had to choose and in general (but not always), we favored Shell over Programming.&nbsp; <\/p>\n<p>&nbsp;One of those areas was error generation.&nbsp; Shells are like the grandparents that you see a couple times a year &#8211; they love you and forgive almost anything.&nbsp; <br \/>Access a variable that doesn&#8217;t exist?&nbsp; We&#8217;re not going to complain.<br \/>Access a property on a variable that doesn&#8217;t exist?&nbsp; That&#8217;s OK.<\/p>\n<p>Now we&#8217;re not idiots so if you try to SET a property on a variable that doesn&#8217;t exist or call a method that doesn&#8217;t exist, we complain.<\/p>\n<p>These are great semantics for ad hoc programming but not for more rigourous programming and they can even burn you for ad hoc programming as well.&nbsp; If you misspell a name, things will run fine but you won&#8217;t get what you want.&nbsp; This is why we introduced STRICT MODE.&nbsp; In V1, we introduced a command SET-PSDEBUG -STRICT which puts PowerShell into strict mode where we complain if you access a variable that does not exist.&nbsp; <\/p>\n<p>That is really great but it has 2 problems.&nbsp; It is not all that strict and it sets the mode for the entire process.&nbsp; Roman Kuzmin brought this up this morning in a comment on the Select-Grid script.&nbsp; He pointed out that it made a call to SET-PSDEBUG -STRICT and warned that this was inappropriate for most environments because it causes your ad hoc scripts to start failing.&nbsp; Get it?&nbsp; So&nbsp;imagine&nbsp;have a sloppy ad hoc script that has these issues but works fine for your purposes.&nbsp; You then dot source Select-Grid and that script now starts throwing errors.&nbsp; Now imagine that it&#8217;s not just one script but dozens of scripts all throwing errors where they didn&#8217;t use to.&nbsp; A purist would say that those are broken scripts and you need to fix them all.&nbsp; A non-purist would say, &#8220;ish!!!&#8221; (an homage to my midwest friends).<\/p>\n<p>If you look at the name, you&#8217;ll see that we were actually pretty explicit about it.&nbsp; This is a technique that is useful for DEBUGGING your script.&nbsp; In V2, we introduce a new cmdlet that addresses many of the problems Set-StrictMode.&nbsp; This&nbsp;provides strict semantics&nbsp;in a way that does not affect the entire process AND it adds a number of new cases to check.&nbsp; We added these cases based upon the complaints we recieved and the errors we saw.&nbsp; That means 2 things: <br \/>1) Yes we listen to you.&nbsp; Please complain complain complain.&nbsp; We LIKE complaints.&nbsp; Every complaint is an opportunity for us to generate the worlds best product.<br \/>2) If we didn&#8217;t cover the case that keeps burning you &#8211; see bullet #1<br \/>We now consider it best practice to use this for scripts of any consequence.&nbsp; Certainly any script that you share with others or post on the internet should use this (to avoid embarrassing yourself [as I do all the time]).<\/p>\n<p>Without further ado, let me introduce you to fabulous new Set-StrictMode:<\/p>\n<p>NAME<br \/>&nbsp;&nbsp;&nbsp; Set-StrictMode<\/p>\n<p>SYNOPSIS<br \/>&nbsp;&nbsp;&nbsp; Establishes and enforces coding rules in expressions, scripts, and script blocks.<\/p>\n<p>SYNTAX<br \/>&nbsp;&nbsp;&nbsp; Set-StrictMode -Off -Version &lt;Version&gt; [&lt;CommonParameters&gt;]<\/p>\n<p>DETAILED DESCRIPTION<br \/>&nbsp;&nbsp;&nbsp; The Set-StrictMode cmdlet configures strict mode for the current scope and turns it on and off. When strict mode is<br \/>&nbsp;&nbsp;&nbsp;&nbsp; on, Windows PowerShell generates a terminating error when the content of an expression, script, or script block vi<br \/>&nbsp;&nbsp;&nbsp; olates basic best-practice coding rules. The strict mode is enforced only in the scope in which you set it.<\/p>\n<p>&nbsp;&nbsp;&nbsp; Set-StrictMode has three settings.<\/p>\n<p>&nbsp;&nbsp;&nbsp; Version 1:<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Prohibits references to uninitialized variables (unless the variables are in strings).<\/p>\n<p>&nbsp;&nbsp;&nbsp; Version 2:<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Prohibits references to uninitialized variables (including variables in strings).<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Prohibits references to non-existent properties of an object.<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Prohibits invalid function syntax (parentheses and commas).<\/p>\n<p>&nbsp;&nbsp;&nbsp; Latest:<br \/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Selects the latest (most strict) version setting.<\/p>\n<p>&nbsp;&nbsp;&nbsp; When Set-StrictMode is off, uninitialized variables (Version 1) are assumed to have a value of 0 or $null, dependin<br \/>&nbsp;&nbsp;&nbsp; g on type. References to non-existent properties return $null. and the results of invalid function syntax vary with<br \/>&nbsp;&nbsp;&nbsp;&nbsp; the error.<\/p>\n<p>Wider and wider the dynamic range of PowerShell grows!<\/p>\n<p>Jeffrey Snover [MSFT]<br \/>Windows Management Partner Architect<br \/>Visit the Windows PowerShell Team blog at:&nbsp;&nbsp;&nbsp; <a href=\"http:\/\/blogs.msdn.com\/PowerShell\">http:\/\/blogs.msdn.com\/PowerShell<\/a><br \/>Visit the Windows PowerShell ScriptCenter at:&nbsp; <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we designed PowerShell we decided that we wanted to&nbsp;have Shell&nbsp;syntax and semantics because this is what was missing from the market and because it supports the ad hoc development practice that is so suited to the needs of administrators.&nbsp; We also observed that most shells ran out of power pretty quickly and people had [&hellip;]<\/p>\n","protected":false},"author":600,"featured_media":13641,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-6141","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>When we designed PowerShell we decided that we wanted to&nbsp;have Shell&nbsp;syntax and semantics because this is what was missing from the market and because it supports the ad hoc development practice that is so suited to the needs of administrators.&nbsp; We also observed that most shells ran out of power pretty quickly and people had [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6141","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/users\/600"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/comments?post=6141"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/6141\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media\/13641"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/media?parent=6141"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=6141"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=6141"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}