This is how PowerShell Variables are designed to work. Variables your scripts or functions set only last as long as they're running, when they end, their values go away.

In what you're doing today, you're changing the $global scope variable but not running a script or function. You're effectively in the global scope already.

In order to use those nested scopes, you need to run a script or function, like this script below, called scratch.ps1

#script inherited the previous value

"script: current favorite animal is $MyFavoriteAnimal, inherited"

#now setting a script level variable, which lasts till the script ends
$MyFavoriteAnimal = "fox"

"script: current favorite animal is $MyFavoriteAnimal"

function GetAnimal(){
   #this function will inherit the variable value already set in the script scope
   "function: my favorite animal is currently $MyFavoriteAnimal"

   #now the function sets its own value for this variable, in the function scope
   $MyFavoriteAnimal = "dog"

   #the value remains changed until the function ends
   "function: my favorite animal is currently $MyFavoriteAnimal"
}

getAnimal

#the function will have ended, so now the script scope value is 'back'
"script: My favorite animal is now $MyFavoriteAnimal"

To access this functionality, you'll need to use scripts or functions.

Answer from FoxDeploy on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_scopes
about_Scopes - PowerShell | Microsoft Learn
January 16, 2026 - The global scope is the root parent scope in a runspace. Local: The current scope. The local scope can be the global scope or any other scope. Script: The scope that's created while a script file runs.
🌐
Substack
myitforum.substack.com › p › powershell-understanding-scope
PowerShell — Understanding Scope - by Rod Trent - myITforum
May 15, 2025 - To mitigate issues, limit the use of global scope to cases where persistence across scripts is necessary. Definition: The script scope applies specifically to variables and commands defined within a single PowerShell script file.
🌐
Reddit
reddit.com › r/powershell › question about scopes and best practices
r/PowerShell on Reddit: Question about scopes and best practices
February 9, 2023 -

about_Scopes

So we have three main scopes, from the looks of things:

  • global

  • local

  • script

I think I understand local; if you do not define a scope, the scope is defaulted to local. I think? Meanwhile, global is accessible to the whole session. I think?

In the context of functions, by default, any variable defined within a function is inaccessible outside of the function. You use return to pass data out of the function.

But, if you declare a variable outside the function with the global scope, you can access it within a function. I’ve used this to declare HashTables and Lists outside the function, and then add elements to the HashTable or List within a function.

Or, if you declare a variable with the global scope within a function, it will also be accessible outside the function. It’s not how I would do things, but a colleague made a pair of functions in a script to connect and disconnect from a database. He declared the database details and credentials at the top of the script using the global scope, and then created the connection within the connection function with the global scope. This allowed him to call the same connection variable in the disconnect function to close the connection. I’d have probably defined the connection outside of the connect function …

But, if I have all my code contained within one script, do I need to use the global scope to make variables accessible inside and outside of functions? Would the script scope work? The explanation in the link at the top suggests it wouldn’t work …

What is this best practice?

🌐
Varonis
varonis.com › blog › powershell-variable-scope
PowerShell Variable Scope Guide: Using Scope in Scripts and Modules
October 13, 2022 - PowerShell’s automatic and preference ... your PowerShell profile are also available in the global scope. Script: This is the scope created when you run a script....
🌐
Reddit
reddit.com › r/powershell › global vs script scope
r/PowerShell on Reddit: Global vs Script scope
September 2, 2015 -

I'm still fairly new to the scripting scene and I've run into a bit of bother with one of my latest scripts. There is a function in the script which passes string values into an array. I would like to call this function numerous times so that the array keeps growing with more and more objects. At the end I want to pass the array on to a different part of my script.

I already know that using the default Local scope for the array won't work, but I want to know which of the Global or Script scope should I use?

🌐
How-To Geek
howtogeek.com › home › files › how scopes affect powershell scripts
How Scopes Affect PowerShell Scripts
November 26, 2014 - In batch scripts, changes to environment variables have a global impact to the current session by default. For PowerShell, the exact opposite is true because scopes are used to isolate a script's modifications.
Find elsewhere
🌐
SS64
ss64.com › ps › syntax-scopes.html
Understanding Scopes in PowerShell
To specify the scope of a new variable, alias, or function, prefix the name with a scope modifier: $global:DemoVar = "hello" $script:DemoVar = "world" $private:DemoVar = "world" function global:DemoFunc {...}
🌐
PowerShell Forums
forums.powershell.org › powershell help
Global Scope, is it local/global or global/global? - PowerShell Help - PowerShell Forums
May 16, 2019 - Hello Everyone, I’ve been reading the About_Scopes help doc: Looking at the definition of “Global Scopes” I have a question Global: The scope that is in effect when PowerShell starts. My question is, does the Globa…
🌐
Adam the Automator
adamtheautomator.com › powershell-scopes
PowerShell Scopes: Understanding Variable Scope
January 27, 2026 - PowerShell’s Global variables are everywhere. For this reason, the common use of PowerShell global variables is to use PowerShell global variables between scripts. There is only one global scope that rules overall.
🌐
Powershellbyexample
powershellbyexample.dev › post › variable-scopes
Scopes | PowerShell By Example
The local scope is relative to whatever context the code runs in at the time. To define an item local you use the local: modifier. By default the scope of a variable is the Local scope. ... Items in the global scope are available everywhere.
🌐
O'Reilly
oreilly.com › library › view › mastering-windows-powershell › 9781787126305 › dbc49d6e-a8d3-4501-92c6-d992cab49f31.xhtml
Script scope - Mastering Windows PowerShell Scripting - Second Edition [Book]
October 27, 2017 - The Script scope is shared across all children in a script or script module. The Script scope is a useful place to store variables which must be shared without exposing the variable to the Global scope (and therefore to anyone with access to ...
Authors   Chris DentBrenton J.W. Blawat
Published   2017
Pages   440
🌐
4sysops
4sysops.com › home › blog › articles › the powershell variable scope
The PowerShell variable scope – 4sysops
July 28, 2023 - If you create a variable on a PowerShell console, its scope is the Global scope. From what I wrote above, you can infer that you can also read its value in the Script scope because the Global scope is the Script scope’s parent scope.
🌐
How-To Geek
howtogeek.com › home › scopes in powershell
Scopes in PowerShell
December 29, 2020 - . 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.
🌐
Mike Frobbins
mikefrobbins.com › 2017 › 06 › 08 › what-is-this-module-scope-in-powershell-that-you-speak-of
What is this Module Scope in PowerShell that you Speak of? | mikefrobbins.com
June 8, 2017 - The third option uses script scope ... of the module. Finally, global scope is tested and it's accessible from another function within the module and from the current scope outside of the module....
🌐
Reddit
reddit.com › r/powershell › help me understand when a var is global vs when it's not.
r/PowerShell on Reddit: Help me understand when a var is global vs when it's not.
February 3, 2023 -

Edit: Thanks for the explainations. My confusion was a misunderstanding of import-modules when I really should be dot-sourcing my functions for the scripts that I write to keep everything within the same scope and have constants that only need to be declared once but used heavily throughout the entire script. So by dot sourcing, I can have the vars that I need to be "global" (I realize how I'm using that word and really mean within same scope) without having to explicitly pass that var each time as a parameter.

Original:

So I'm having a lil bit of confusion around when my vars are global in scope vs when they aren't. So say I'm writing a called "Script.ps1" and I create a module file and call that "Logger.psm1" and use an import-module statement in Script.ps1. Logger.psm1 has a custom function called write-log.

So now say I declare a var in Script.ps1. This var is something that is a constant and will never change. Like a logging directory. Now we have the following files:

Script.ps1:

import-module ".\Logger.psm1"

$LogDirectory = "Some\File\Path"

write-log -message "Test Message"

Logger.psm1:

function Write-Log {
    param (
        $message
    )
    $messsage | Out-file -path "$LogDirectory\logfile.log"
}

In this setup I expect $LogDirectory to be a global var that is accessible by the module I imported and calling from Script.ps1. But the behavior I see is $LogDirectory isn't treated as a global var. Instead I have to actively declare $LogDirectory as a global using $global:Logdirectory for the module to see it. Otherwise I have to add it as a parameter.

So few questions:

  • Why is this?

  • Does the module have a separate global scope from the ps1 script that imported it?

  • What's the best practice for accomplishing something like this? I have another script that I think is suffering from a similar issue.

Top answer
1 of 5
10
Unless you explicitly declare a variable with a different scope, a variable will only be available for use where it's declared. E.g., if declared in a cmdlet/function then it can only be used by what's contained within that cmdlet/function. Creating a function within a cmdlet does not let you use the variables declared in the cmdlet in the function and vice versa--you would have to pass values through params. Declaring something as global allows it to be used by anything else running within the runspace and it persists after the cmdlet/function terminates. In your example, I would just add a log directory parameter to write-log. This would make your function more flexible since you could change where something gets logged on the fly instead of being stuck only being able to write to a single location.
2 of 5
4
Perhaps the way I use it can provide an example. I wrote a function to capture the nested structure of the groups in my org. It loops through the membership and when it finds a group, it makes a call back to itself. Basically, something like… Show-nestingchart When it finds a nested group it calls to Show-nestingchart, basically itself, to loop through that membership. It returns an output like this: Member. Member. Member. Member. NestedGroup\member. NestedGroup\member. NestedGroup2\member. NestedGroup3\member. NestedGroup\anothergroup\member. Each time it calls to itself, each instance has variables scoped locally to it. The results are written to a $global:results held in the 1st instance.
🌐
SharePoint Diary
sharepointdiary.com › sharepoint diary › powershell › powershell global variables: a comprehensive guide
PowerShell Global Variables: A Comprehensive Guide - SharePoint Diary
October 9, 2025 - Because they have a wider scope, global variables are suitable for storing data that different functions or scripts need to share. In PowerShell, variable scope relates to where a variable can be accessed.
Top answer
1 of 2
4

Starting with the latter question:

Scopes come in play with functions and invoked scripts(cmdlets), like:

Function Test {
    $Test++
    Write-Host 'Local:' $Test
}
$Test = 5
Test
Write-Host 'Global:' $Test

Returns:

Local: 6
Global: 5

And:

Function Test {
    $Global:Test++
    Write-Host 'Local:' $Test
}
$Test = 5
Test
Write-Host 'Global:' $Test

Returns:

Local: 6
Global: 6

Or if you put the function in a script (e.g. MyScript.ps1):

$Test = 5
.\MyScript.ps1
Write-Host $Test # $Test is unaffected unless you use the $Global scope in your script

Which will return basically the same results as above, unless you Dot-Source your script where it will run in the current scope:

$Test = 5
. .\MyScript.ps1
Write-Host $Test # $Test might be affected by MyScript.ps1 if you just use $Test

For what you are doing:
You are creating a complete new PowerShell session (with Powershell.exe) which will start with a fresh list of variables.
Note here that you will see the initial variables again if you exit from the new session:

PS C:\> $Name = "John"
PS C:\> Powershell.exe
Windows PowerShell
Copyright (C) Microsoft Corporation. All rights reserved.

Try the new cross-platform PowerShell https://aka.ms/pscore6

PS C:\> Write-Host 'New session' $Name
New session
PS C:\> Exit
PS C:\> Write-Host 'Initial session' $Name
Initial session John

Which regards to the first question, I don't think that there are many applications where you need to explicitly refer to the $Local scope, but to give you an example where you might use it:

$Test = 5
Function Test {
    Write-Host ($Local:Test++)
}
Test

In the above example the unary increment operator will start with 0 if you explicitly use the $Local scope (in fact you starting with an empty local variable which will cast to 0) and with 5 if you omit the $Local scope where you will inherit a copy of the $Test variable out of the parent scope.

2 of 2
3

To complement iRon's helpful answer:

  1. [...] when will we need to explicitly specify the local modifier?

$local: is rarely required, because the local scope is implied in the absence of a scope specifier.

However, that only applies if the referenced variable actually exists as a local variable, given that PowerShell's dynamic scoping makes variables from ancestral (parent) scopes visible to descendant (child) scopes as well (see this answer for more information):

  • For example, say you have $foo = 'bar' declared in the global scope, then referring to $foo in a script would look for a local $foo instance first; if there is none, a $foo defined in an ancestral (parent) scope is used, if any, which would be the global $foo in this example, and 'bar' would be returned.

  • By contrast, if, in your script, you use $local:foo, without a local $foo variable being defined, you either get $null by default or, if Set-StrictMode -Version 2 or higher is in effect, a statement-terminating error occurs.


  1. MSDN says: [...] by creating a session, or by starting a new instance of PowerShell [...] the result is a parent scope (the original scope) and a child scope (the scope that you created).

The documentation is incorrect in this regard as of this writing (a GitHub issue has been filed):

  • Ancestral (parent-child) relationships between scopes exist only in the context of a given session (runspace).

    • That is, dynamic scoping - the visibility of variables and other definitions from ancestral scopes - only applies to scopes within a given session.

    • A notable exception is that functions from a module do not run in a child scope of the calling scope - except if the that calling scope happens to be the global scope; modules have their own scope domains (technically called session states) that are linked to the global scope only - see this GitHub docs issue for a discussion.

  • Therefore, no child scope of the calling scope is created in the following scenarios, where the newly launched code knows nothing of the variables (and other definitions) in the calling scope:

    • Starting a new session via PowerShell remoting (e.g., with Enter-PSSession) or Invoke-Command -Computer

    • Starting a background [thread] job with Start-Job or Start-ThreadJob or running threads in parallel with ForEach-Object -Parallel in v7.0+

    • Starting a new PowerShell instance (process), using the PowerShell CLI (pwsh for PowerShell [Core], powershell.exe for Windows PowerShell).

    • To communicate values from the calling scope to the newly launched code in these scenarios, explicit action is required:

      • When calling the CLI or using Start-Job, where a child process on the same machine is created, only environment variables defined in the calling process become automatically available to the child process.
      • Otherwise, values from the caller must be passed as arguments or - except when using the CLI - via the $using: scope - see this answer.