{"id":143472,"date":"2020-12-29T16:00:13","date_gmt":"2020-12-29T13:00:13","guid":{"rendered":"https:\/\/en.buradabiliyorum.com\/scopes-in-powershell-cloudsavvy-it\/"},"modified":"2020-12-29T16:00:13","modified_gmt":"2020-12-29T13:00:13","slug":"scopes-in-powershell-cloudsavvy-it","status":"publish","type":"post","link":"https:\/\/buradabiliyorum.com\/en\/scopes-in-powershell-cloudsavvy-it\/","title":{"rendered":"#Scopes in PowerShell \u2013 CloudSavvy IT"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a3f636cce6d1\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #dd3333;color:#dd3333\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #dd3333;color:#dd3333\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a3f636cce6d1\" checked aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/buradabiliyorum.com\/en\/scopes-in-powershell-cloudsavvy-it\/#What_are_the_PowerShell_Scopes\" >What are the PowerShell Scopes?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/buradabiliyorum.com\/en\/scopes-in-powershell-cloudsavvy-it\/#What_is_the_Private_Using_Workflow_and_AllScope_options\" >What is the Private, Using, Workflow, and AllScope options?<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/buradabiliyorum.com\/en\/scopes-in-powershell-cloudsavvy-it\/#The_Private_Scope_Modifier\" >The Private Scope Modifier<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/buradabiliyorum.com\/en\/scopes-in-powershell-cloudsavvy-it\/#What_is_the_Using_Modifier\" >What is the Using Modifier?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/buradabiliyorum.com\/en\/scopes-in-powershell-cloudsavvy-it\/#Workflow_and_AllScope\" >Workflow and AllScope<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/buradabiliyorum.com\/en\/scopes-in-powershell-cloudsavvy-it\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<p><strong>&#8220;#Scopes in PowerShell \u2013 CloudSavvy IT&#8221;<\/strong><\/p>\n<div id=\"article-content-area\">\n<img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-4374\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/8fec593cd19e3b20a0773cafaaaaf848\/p\/uploads\/2020\/03\/23e4a5a4.png\" alt=\"Powershell logo\" width=\"1400\" height=\"578\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>In many programming languages, there is the concept of scope. Defined as what variables and functions are available in what context. An example is if a variable is available to every function within a PowerShell <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/download-scripts-themes-apps\/\" data-internallinksmanager029f6b8e52c=\"9\" title=\"Download Scripts &amp; Themes &amp; Apps\" target=\"_blank\" rel=\"noopener\">script<\/a>, or just a single defined function. <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/redirect.viglink.com?u=https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fmodule%2Fmicrosoft.powershell.core%2Fabout%2Fabout_scopes%3Fview%3Dpowershell-7.1&amp;key=204a528a336ede4177fff0d84a044482\">PowerShell Scopes<\/a> encompass several different scopes that can be used such as Global, Local, or Script scopes.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_are_the_PowerShell_Scopes\"><\/span>What are the PowerShell Scopes?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>As mentioned in the introduction, there are several primary scopes. These are global, local, and script and tell PowerShell what variables and functions are available in what context. When referencing scopes, you usually use scope prefixes to reach variables outside the current, local, scope. This is easiest to understand through examples.<\/p>\n<p><strong>Global Scope<\/strong><\/p>\n<p>Global scope means that a variable will be available for all functions and commands regardless of location, such as <code>$MyVar = $True<\/code> or <code>$Global:MyVar = $True<\/code>. Upon launching PowerShell, all initial variables are globally scoped. If there are no child scopes, global is the same as local and script scope until child scopes exist.<\/p>\n<pre><code class=\"language-powershell\"># This is in the Global scope&#13;\n$MyVar = $True&#13;\n&#13;\nFunction Test-VariableScope {&#13;\n    # When a variable is not found in the current scope, look up till it is found&#13;\n    Write-Host \"Global Scope: $MyVar\"&#13;\n    # Redefine the variable in the Local scope&#13;\n    $MyVar = $False&#13;\n    Write-Host \"Local Scope: $MyVar\"&#13;\n    Write-Host \"Global Scope: $($Global:MyVar)\"&#13;\n}&#13;\n&#13;\nTest-VariableScope&#13;\n&#13;\n# Since the Locally scoped variable does not exist anymore, return the Global scope&#13;\nWrite-Host \"Global Scope: $MyVar\"&#13;\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8717\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/2c4fcb811e9a2e9debbed39d4b917b14\/p\/uploads\/2020\/12\/0b02ecf4.png\" alt=\"\" width=\"1058\" height=\"397\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p><strong>Local Scope<\/strong><\/p>\n<p>The current scope is always local, and therefore the default scope as well. When no scope modifier is used on a variable, the scope is local such as <code>$MyVar = $True<\/code>. The local scope can be hard to understand as this scope is relative to the current location and a local variable can be in different scopes at the same time.<\/p>\n<pre><code class=\"language-powershell\"># This is in the Local scope (and Global scope since at root of script)&#13;\n$MyVar = $True&#13;\nWrite-Host \"Local Scope: $MyVar\"&#13;\nWrite-Host \"Local Scope Explicit: $($Local:MyVar)\"&#13;\nFunction Test-VariableScope {&#13;\n    # This variable is in the Local scope relative to Test-VariableScope&#13;\n    $MyVar = $False&#13;\n    Write-Host \"Local Scope: $MyVar\"&#13;\n    Write-Host \"Local Scope (Explicit): $($Local:MyVar)\"&#13;\n}&#13;\n&#13;\nTest-VariableScope&#13;\n# This variable is back to the local scope initially defined&#13;\nWrite-Host \"Local Scope: $MyVar\"&#13;\nWrite-Host \"Local Scope Explicit: $($Local:MyVar)\"&#13;\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8718\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/34ebd5ae51b53f8cc97844726b1f5113\/p\/uploads\/2020\/12\/f7b2df16.png\" alt=\"\" width=\"1158\" height=\"426\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<blockquote><p><em>When you dot-source a script, such as <code>. my-script.ps1<\/code>, you will add the entire output of that script to the local scope!<\/em><\/p>\n<\/blockquote>\n<p><strong>Script Scope<\/strong><\/p>\n<p>Finally, the script scope is a bit more complicated. It is either the nearest ancestor script file scope or global scope if not run in a script file such as <code>$script:var = $true<\/code>. Global, Script, and Local scope are the same for the <code>$MyVar = $True<\/code> variable when used in a single script, and even in functions.<\/p>\n<pre><code class=\"language-powershell\"># This is in the Script scope including Script and Local scope&#13;\n$MyVar = $True&#13;\nWrite-Host \"Script Scope: $($Script:MyVar)\"&#13;\n&#13;\nFunction Test-VariableScope {&#13;\n    # This variable is in the Local scope relative to Test-VariableScope&#13;\n  $MyVar = $False&#13;\n  # The Script scope references the same as $MyVar variable initially defined&#13;\n    Write-Host \"Script Scope: $($Script:MyVar)\"&#13;\n}&#13;\n&#13;\nTest-VariableScope&#13;\n# This variable is back to the Script scope initially defined&#13;\nWrite-Host \"Script Scope: $($Script:MyVar)\"&#13;\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8719\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/e59b1c517c9bf11bf6ab6422aeaf4dbb\/p\/uploads\/2020\/12\/cb9fd08c.png\" alt=\"\" width=\"1037\" height=\"355\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<p>Unlike dot-sourcing a script which adds everything to the local scope, using the <code>&amp;<\/code> call operator will run the script, but leave everything in the script scope which is relative to the script itself.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"What_is_the_Private_Using_Workflow_and_AllScope_options\"><\/span>What is the Private, Using, Workflow, and AllScope options?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Along with the <a href=\"https:\/\/buradabiliyorum.com\/en\/category\/general\/\" data-internallinksmanager029f6b8e52c=\"3\" title=\"General\" target=\"_blank\" rel=\"noopener\">general<\/a> scopes, there are several options that can modify the visibility or availability of variables. So what are these different options with scopes?<\/p>\n<h3><span class=\"ez-toc-section\" id=\"The_Private_Scope_Modifier\"><\/span>The Private Scope Modifier<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>When you define a variable or alias as private, the variable or alias is only visible and modifiable from within that context. As you can see from the example below, the private version of the<\/p>\n<pre><code class=\"language-powershell\">Remove-Variable TestVar&#13;\nRemove-Variable TestVar2&#13;\nRemove-Variable TestVar3&#13;\n&#13;\n# Two ways to create a private variable, note that this is an Option and not a Scope (applied to a scoped variable)&#13;\nNew-Variable -Name 'TestVar' -Value $True -Scope 'Global' -Option 'Private'&#13;\n$Private:TestVar2 = $True&#13;\n&#13;\nWrite-Host \"Global Scope (TestVar): $TestVar\"&#13;\nWrite-Host \"Global Scope (TestVar2): $TestVar\"&#13;\n&#13;\n$TestVar3 = $True&#13;\n&#13;\nFunction Test-Function {&#13;\n  # Since the first two variables are private they are not seen&#13;\n  Write-Host \"Local Scope (TestVar): $TestVar\"&#13;\n  Write-Host \"Local Scope (TestVar2): $TestVar2\"&#13;\n  # TestVar3 does not exist locally so PowerShell looks up and finds a non-Private variable in Global&#13;\n  Write-Host \"Local Scope (TestVar3): $TestVar3\"&#13;\n}&#13;\n&#13;\nTest-Function&#13;\n<\/code><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8720\" src=\"https:\/\/www.cloudsavvyit.com\/thumbcache\/0\/0\/834efba766189e24b1032c7003af35b8\/p\/uploads\/2020\/12\/37a19f67.png\" alt=\"\" width=\"1160\" height=\"404\" onload=\"pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\" onerror=\"this.onerror=null;pagespeed.lazyLoadImages.loadIfVisibleAndMaybeBeacon(this);\"\/><\/p>\n<h3><span class=\"ez-toc-section\" id=\"What_is_the_Using_Modifier\"><\/span>What is the Using Modifier?<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>This is a special variable that is used by the cmdlets and constructs <code>Invoke-Command<\/code>, <code>Start-Job<\/code>, <code>Start-ThreadJob<\/code>, or <code>ForEach-Object -Parallel<\/code>. This will only work with <code>Invoke-Command<\/code> when it is being run on a remote computer such as with the <code>ComputerName<\/code> parameter.<\/p>\n<pre><code class=\"language-powershell\"># Normally the LocalCSVData variable will not be available to the scriptblock running on the remote computer without the Using modifier.&#13;\n$LocalCSVData = Import-CSV -Path 'mydata.csv'&#13;\n&#13;\nInvoke-Command -ComputerName 'RemoteComputer' -ScriptBlock {&#13;\n    $Using:LocalCSVData | ForEach-Object {&#13;\n        ...do something...&#13;\n    }&#13;\n}&#13;\n<\/code><\/pre>\n<h3><span class=\"ez-toc-section\" id=\"Workflow_and_AllScope\"><\/span>Workflow and AllScope<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Although much less used, there are two additional modifiers. The <code>Workflow<\/code> modifier is used to reference variables in a PowerShell Workflow (not available in PowerShell Core\/7). The <code>AllScope<\/code> modifier will make a variable available in all scopes. This also means that if the variable is modified in a child scope it will reflect in the root scopes. This is one way to avoid having to prefix a variable with global when attempting to reference it in child scopes.<\/p>\n<h2 role=\"heading\" aria-level=\"2\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Scopes are useful to limiting the visibility and availability of variables, functions, and aliases. Scopes can also be troublesome when it is not clear what scope a variable is in. Hopefully this article sheds some light on how scopes are used in PowerShell and what you can do with them!\n<\/p><\/div>\n<blockquote><p><strong><span style=\"color: #ff6600;\">If you liked the article, do not forget to share it with your friends. Follow us on\u00a0<span style=\"color: #ff0000;\"><a style=\"color: #ff0000;\" href=\"https:\/\/news.google.com\/publications\/CAAqBwgKMLG0nwswvr63Aw\" target=\"_blank\" rel=\"nofollow noopener noreferrer\">Google News<\/a><\/span>\u00a0too, click on the star and choose us from your favorites.<\/span><\/strong><\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\">For forums sites go to <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/forum.buradabiliyorum.com\/\" target=\"_blank\" rel=\"noopener\">Forum.BuradaBiliyorum.Com<\/a><\/span><\/strong>\n<\/p><\/blockquote>\n<blockquote>\n<p style=\"text-align: center;\"><strong>If you want to read more like this article, you can visit our <span style=\"color: #ff9900;\"><a style=\"color: #ff9900;\" href=\"https:\/\/en.buradabiliyorum.com\/technology\/\" target=\"_blank\" rel=\"noopener\">Technology category.<\/a><\/span><\/strong><\/p>\n<\/blockquote>\n<p><span style=\"color: black;\"><a style=\"color: #ff9900;\" href=\"https:\/\/www.cloudsavvyit.com\/8716\/scopes-in-powershell\/\" target=\"_blank\" rel=\"noopener\">Source<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;#Scopes in PowerShell \u2013 CloudSavvy IT&#8221; In many programming languages, there is the concept of scope. Defined as what variables and functions are available in what context. An example is if a variable is available to every function within a PowerShell script, or just a single defined function. PowerShell Scopes encompass several different scopes that&#8230;<\/p>\n","protected":false},"author":1,"featured_media":143473,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"fifu_image_url":"https:\/\/www.cloudsavvyit.com\/p\/uploads\/2020\/03\/23e4a5a4.png","fifu_image_alt":"","footnotes":""},"categories":[18],"tags":[],"class_list":["post-143472","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technology"],"_links":{"self":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/143472","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/comments?post=143472"}],"version-history":[{"count":0,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/posts\/143472\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media\/143473"}],"wp:attachment":[{"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/media?parent=143472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/categories?post=143472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/buradabiliyorum.com\/en\/wp-json\/wp\/v2\/tags?post=143472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}