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?

🌐
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?

🌐
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....
🌐
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…
🌐
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.
🌐
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.
🌐
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.
🌐
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
🌐
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....
🌐
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.
🌐
Datatechguard
datatechguard.com › home › powershell variable scope guide: using scope in scripts
PowerShell Variable Scope Guide: Using Scope in Scripts
December 4, 2025 - The global scope is in effect when PowerShell starts and is accessible everywhere. The local scope, on the other hand, represents the current scope and can be the global scope or any other scope within the script.