{"id":5782,"date":"2013-03-06T11:23:00","date_gmt":"2013-03-06T19:23:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=5782"},"modified":"2013-03-06T11:23:57","modified_gmt":"2013-03-06T19:23:57","slug":"first-rule-of-powershell-scoping-rules","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2013\/03\/06\/first-rule-of-powershell-scoping-rules\/","title":{"rendered":"First rule of PowerShell scoping rules&hellip;"},"content":{"rendered":"<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/powershellscopingsoap.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"powershell scoping soap\" border=\"0\" alt=\"powershell scoping soap\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/powershellscopingsoap_thumb.png\" width=\"644\" height=\"454\"><\/a> <\/p>\n<p>\u2026 never talk about PowerShell scoping rules.<\/p>\n<p>Judging from the posts, emails and questions we get about variable scope in PowerShell Studio forms, PowerShell scoping rules are shrouded in mystery. When you read <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/hh847849.aspx\" target=\"_blank\">about_scopes<\/a> you get a lot of information. When you come from any other scripting language, for example VBScript, you are used to two scopes; global and local. The default scope of a variable in VBScript depends on its first use or its declaration. So if you want a variable to have a specific scope you just declare it with a DIM statement.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image1.png\" target=\"_blank\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image_thumb1.png\" width=\"644\" height=\"438\"><\/a> <\/p>\n<\/p>\n<p>This code sample illustrates the simple scoping rules in VBScript. A variable is global or local and yes, a local variable can shadow a global one with the same name.<\/p>\n<p>The output is very much what you expect:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image2.png\" width=\"487\" height=\"204\"> <\/p>\n<p>&nbsp;<\/p>\n<p>Now let\u2019s do that in PowerShell:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image3.png\" width=\"420\" height=\"460\"> <\/p>\n<p>And the result is:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image4.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image_thumb2.png\" width=\"272\" height=\"169\"><\/a> <\/p>\n<p>The keen observer will immediately notice that $globalvariable never produces any output. That is because a variable in PowerShell is not global unless you specifically says so by using $global:globalvariable. Always. At every use. So what is the scope of $globalvariable in our example? It\u2019s \u2018local\u2019, local to the global scope to be precise. Unless specified otherwise nothing is ever global.<\/p>\n<p>So how do you set a variable from a child context? <\/p>\n<p>1. You can use a return value of a function<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image5.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image_thumb3.png\" width=\"222\" height=\"144\"><\/a> <\/p>\n<p>2. You can pass&nbsp; the variable as a parameter reference<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image6.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image_thumb4.png\" width=\"416\" height=\"249\"><\/a> <\/p>\n<p>Note that you cannot just assign a value to $globalvariable, that would just create a local variable of that name shadowing the parameter. You are passing a reference object, so you have to use the <em>value<\/em> property.<\/p>\n<p>3. Use $global: or $script: to modify the scope of the variable.<\/p>\n<p>If you are five levels removed from the definition or in a completely different context, for example in the event handler for a button in a Windows form, this might be the simplest solution.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image7.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px\" title=\"image\" border=\"0\" alt=\"image\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2013\/03\/image_thumb5.png\" width=\"433\" height=\"207\"><\/a> <\/p>\n<p>&nbsp;<\/p>\n<p>One last final remark and we will not talk about this anymore. Read the about article on scope. Try the examples. You will need it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u2026 never talk about PowerShell scoping rules. Judging from the posts, emails and questions we get about variable scope in PowerShell Studio forms, PowerShell scoping rules are shrouded in mystery. When you read about_scopes you get a lot of information. When you come from any other scripting language, for example VBScript, you are used to [&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":[27,25],"tags":[811,810,28,809,380,808,996],"class_list":["post-5782","post","type-post","status-publish","format-standard","hentry","category-ask-the-experts","category-windows-powershell","tag-global","tag-local","tag-powershell","tag-rules","tag-scope","tag-scoping","tag-vbscript"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/5782","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=5782"}],"version-history":[{"count":3,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/5782\/revisions"}],"predecessor-version":[{"id":5794,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/5782\/revisions\/5794"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=5782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=5782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=5782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}