{"id":473,"date":"2008-07-09T12:39:44","date_gmt":"2008-07-09T20:39:44","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/07\/09\/powershell-and-schrdingers-cat\/"},"modified":"2008-07-10T05:26:43","modified_gmt":"2008-07-10T13:26:43","slug":"powershell-and-schrdingers-cat","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/07\/09\/powershell-and-schrdingers-cat\/","title":{"rendered":"PowerShell and Schr&ouml;dinger&#8217;s cat"},"content":{"rendered":"<p>Powershell does not require you to declare variables. For a shell, that makes perfect sense because that would create too much overhead for simple one-off tasks.<\/p>\n<p>However, as soon as you start to develop more complex scripts, it is very important to know what scope your dynamically created variables have. Consider the following VBScript:<\/p>\n<p><font face=\"Courier New\">Function Printlocals<br \/>\n&nbsp;&nbsp;&nbsp; WScript.Echo LocalVar1<br \/>\n&nbsp;&nbsp;&nbsp; WScript.Echo LocalVar2<br \/>\nEnd Function <\/font><\/p>\n<p><font face=\"Courier New\">Function Localvariables<br \/>\n&nbsp;&nbsp;&nbsp; LocalVar1 = &quot;Hello&quot;<br \/>\n&nbsp;&nbsp;&nbsp; LocalVar2 = &quot;World&quot;<br \/>\n&nbsp;&nbsp;&nbsp; Printlocals<br \/>\nEnd Function <\/font><\/p>\n<p><font face=\"Courier New\">Localvariables<br \/>\nprintlocals<\/font><\/p>\n<p>The output of this script is absolutely nothing, except for some empty lines. Because the variables LocalVar1 and LocalVar2 are always empty in the context of the function Printlocals<\/p>\n<p>No big surprise here. So let&#8217;s do the same thing in PowerShell<\/p>\n<p><font face=\"Courier New\">function print-locals<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; &quot;$localvar1&quot;<br \/>\n&nbsp;&nbsp;&nbsp; &quot;$localvar2&quot;<br \/>\n} <\/font><\/p>\n<p><font face=\"Courier New\">function local-vars()<br \/>\n{<br \/>\n&nbsp;&nbsp;&nbsp; $localvar1 = &quot;Hello&quot;<br \/>\n&nbsp;&nbsp;&nbsp; $localvar2 = &quot;World&quot;<br \/>\n&nbsp;&nbsp;&nbsp; Print-locals<br \/>\n} <\/font><\/p>\n<p><font face=\"Courier New\">local-vars<br \/>\nprint-locals<\/font><\/p>\n<p>What we get as output here is the following:<\/p>\n<p><em>Hello<br \/>\nWorld <\/em><\/p>\n<p>This output comes from the first call to Print-Locals after setting their value inside Local-Vars. The variables are empty again when you call Print-locals again from any other context.<\/p>\n<p>It is important to note that the result is the same if you use the $local:localvar1 notation.<\/p>\n<p>So the scope of those variables is neither global nor local in the traditional sense. One could now assume that you cannot know what the content or scope of a variable is until you try to access it, just like the state of the cat in Schr&ouml;dinger&#8217;s thought experiment.<\/p>\n<p>Judging from this experiment it appears that local scope variables in PowerShell are of &quot;downstream&quot; scope, meaning that they are accessible to anything that is called afterwards, but go out of scope once the code returns from the context in which they are created.<\/p>\n<p>This is an important difference to other scripting languages and can make for some interesting debugging sessions should you forget about this.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Powershell does not require you to declare variables. For a shell, that makes perfect sense because that would create too much overhead for simple one-off tasks. However, as soon as you start to develop more complex scripts, it is very important to know what scope your dynamically created variables have. Consider the following VBScript: Function [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[25],"tags":[28,224,57],"class_list":["post-473","post","type-post","status-publish","format-standard","hentry","category-windows-powershell","tag-powershell","tag-schrodinger","tag-scripting"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/473","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=473"}],"version-history":[{"count":0,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/473\/revisions"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=473"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=473"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=473"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}