{"id":11011,"date":"2006-04-25T12:17:39","date_gmt":"2006-04-25T12:17:39","guid":{"rendered":"https:\/\/blogs.msdn.microsoft.com\/powershell\/2006\/04\/25\/did-you-know-2\/"},"modified":"2019-02-18T13:25:09","modified_gmt":"2019-02-18T20:25:09","slug":"did-you-know-2","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/powershell\/did-you-know-2\/","title":{"rendered":"Did you know? #2"},"content":{"rendered":"<p>Did you know that you can make a variable automatically propagate to new scopes by using the AllScope option?<\/p>\n<p>Normally when you enter a new scope variables are not copied from the parent <br \/>scope. Instead we do a lookup for the variable when requested. Anytime the <br \/>variable is written to it happens in the local scope. This means that it is <br \/>possible to hide constant variables in a parent scope which can lead to <br \/>programming mistakes.&nbsp; The AllScope option automatically copies the variable <br \/>to any new child scope which prevents you from accidentally hiding your <br \/>constant variable.<\/p>\n<p>Example without AllScope:<br \/>MSH &gt; function varInScope { new-variable var -value &#8220;new value&#8221;; $var }<br \/>MSH &gt; new-variable var -value &#8220;original value&#8221; -option Constant<br \/>MSH &gt; $var<br \/>original value<br \/>MSH &gt; varInScope<br \/>new value<\/p>\n<p>Example using AllScope:<br \/>MSH &gt; function varInScope { new-variable var -value &#8220;new value&#8221;; $var }<br \/>MSH &gt; new-variable var -value &#8220;original value&#8221; -option &#8220;Constant,AllScope&#8221;<br \/>MSH &gt; $var<br \/>original value<br \/>MSH &gt; varInScope<br \/>new-variable : A variable with name &#8216;var&#8217; already exists.<br \/>At line:1 char:35<br \/>+ function varInScope { new-variable&nbsp; &lt;&lt;&lt;&lt; var -value &#8220;new value&#8221;; $var }<br \/>original value<\/p>\n<p>-Jeff Jones<\/p>\n<p>[<i>Edit: Monad has now been renamed to Windows PowerShell.  This script or discussion may require slight adjustments before it applies directly to newer builds.<\/i>]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Did you know that you can make a variable automatically propagate to new scopes by using the AllScope option? Normally when you enter a new scope variables are not copied from the parent scope. Instead we do a lookup for the variable when requested. Anytime the variable is written to it happens in the local [&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-11011","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell"],"acf":[],"blog_post_summary":"<p>Did you know that you can make a variable automatically propagate to new scopes by using the AllScope option? Normally when you enter a new scope variables are not copied from the parent scope. Instead we do a lookup for the variable when requested. Anytime the variable is written to it happens in the local [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/11011","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=11011"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/posts\/11011\/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=11011"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/categories?post=11011"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/powershell\/wp-json\/wp\/v2\/tags?post=11011"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}