🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_scopes
about_Scopes - PowerShell | Microsoft Learn
January 16, 2026 - Explains the concept of scope in PowerShell and shows how to set and change the scope of elements. PowerShell protects access to variables, aliases, functions, and PowerShell drives (PSDrives) by limiting where they can be read and changed.
🌐
SS64
ss64.com › ps › syntax-scopes.html
Define the Scope of a variable. - PowerShell
PowerShell scopes protect access to variables, aliases, functions, and PowerShell drives (PSDrives) by limiting where they can be read and changed.
Discussions

Referring to variables created in a function
Hi- Trying to grasp the idea of variable scopes. I know a variable only exists inside a function and you would need to add $script:variablename to get the variable to be seen outside the function. So, from within the function and outside the function when I need the variable value do I always ... More on forums.powershell.org
🌐 forums.powershell.org
0
0
June 15, 2020
PowerShell — Understanding Scope - by Rod Trent - myITforum
When working with PowerShell scripts and modules, understanding variable scope is paramount to writing efficient, reliable, and maintainable code. However, the distinctions between global, script, and local scopes often lead to confusion. More on myitforum.substack.com
🌐 myitforum.substack.com
May 15, 2025
Question about scopes and best practices
IMO it's best to ignore the scope functionality and instead exclusively use parameters and normal output objects to handle moving data between functions. One exception to this would be if you are creating a module like VMware.PowerCli where you are connecting to a service, in that case I would use the script scope to persist the variable across the entire module scope and I would define my functions like this: function Get-MyService { $Res = $Script:MyService if ($null -eq $Res) { throw "You are not connected" } $Res } function Connect-MyService ($Creds, $Target) { try { Invoke-RestMethod -ErrorAction Stop # Use Creds and Target $Script:MyService } catch { throw $_ } } function Do-Something ($Connection = (Get-MyService -ErrorAction Stop)) { } More on reddit.com
🌐 r/PowerShell
10
1
February 9, 2023
Scope Variable usage
I have this script that places a variable to adjust for PS 2.0 but as my DCs are all higher, wanted to try it with a Remote Variable ($Using: ) I read this: about Remote Variables - PowerShell | Microsoft Docs …but not seeing how to adapt for my particular script. More on forums.powershell.org
🌐 forums.powershell.org
0
0
November 16, 2020
🌐
Varonis
varonis.com › blog › powershell-variable-scope
PowerShell Variable Scope Guide: Using Scope in Scripts and Modules
October 13, 2022 - I defined the $greeting variable in the global console scope as “Hello, Jeff!”. I then ran the script file, which reassigned the value to “Hello, World!” using the $global: scope modifier. After running the script, the value of $greeting has been modified in the global console scope to the script’s value. A PowerShell module is a package of commands, such as cmdlets or functions, that have similar functions or purposes.
🌐
PowerShell Forums
forums.powershell.org › powershell help
Referring to variables created in a function - PowerShell Help - PowerShell Forums
June 15, 2020 - Hi- Trying to grasp the idea of variable scopes. I know a variable only exists inside a function and you would need to add $script:variablename to get the variable to be seen outside the function. So, from within the fu…
🌐
SPGuides
spguides.com › powershell-global-variable
How to Use PowerShell Global Variables?
March 26, 2025 - Global Scope: Variables defined with the $global: prefix are considered global and are accessible across all scripts, functions, or cmdlets within the current PowerShell session.
🌐
clan8blog
clan8blog.wordpress.com › 2013 › 11 › 06 › be-careful-with-that-scope
Be careful with that scope | clan8blog
May 3, 2020 - A variable in the main script, if its not inside a function is is ‘script scope’ unless you explicitly define it as global or Private using $global:MyVariable or $private:MyVariable. All child functions have ‘read access’ to the variables created by the parent or grandparent or great gran parent or …. you get the idea. If you don’t prefix the variable then PowerShell will search for the variable in the ‘Local Scope’ first , then ‘Script Scope’ and then finally ‘Global Scope’ before giving up and sulking.
Find elsewhere
🌐
Ironman Software
blog.ironmansoftware.com › daily-powershell › assign-powershell-variables
Assign Variables in PowerShell
November 22, 2021 - Script scope is available to the current script file or module. $Global:Variable = 123 $Script:Variable = 123 Set-Variable -Name Variable -Value 123 -Scope Global Set-Variable -Name Variable -Value 123 -Scope Script
🌐
4sysops
4sysops.com › home › blog › articles › the powershell variable scope
The PowerShell variable scope – 4sysops
July 28, 2023 - Likewise, if you define a variable in a function, you cannot access its value at the script level where you called the function. The variable’s scope is responsible for this behavior; it determines where the variable is available. Scopes are organized in hierarchies. At the top sits the Global scope. This scope is created whenever you start a PowerShell session.
🌐
Substack
myitforum.substack.com › p › powershell-understanding-scope
PowerShell — Understanding Scope - by Rod Trent - myITforum
May 15, 2025 - Always prefer local scope for variables that do not require persistence or access beyond the function or block. Scopes are hierarchical. When PowerShell encounters a variable or command, it first checks the local scope, then the script scope, and finally the global scope.
🌐
Powershelluniversal
docs.powershelluniversal.com › apps › custom-variable-scopes
Custom Variable Scopes | PowerShell Universal
January 6, 2026 - Once assigned, the $Cache:Computer variable is available within any endpoint. ... Page scope stores variables in per browser tab or window.
🌐
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?

🌐
PowerShell Forums
forums.powershell.org › powershell help
Scope Variable usage - PowerShell Help - PowerShell Forums
November 16, 2020 - I have this script that places a variable to adjust for PS 2.0 but as my DCs are all higher, wanted to try it with a Remote Variable ($Using:<VariableName>) I read this: about Remote Variables - PowerShell | Microsoft D…
🌐
How-To Geek
howtogeek.com › home › files › how scopes affect powershell scripts
How Scopes Affect PowerShell Scripts
November 26, 2014 - The Global scope is the scope that is created when PowerShell starts. It includes the variables, aliases, functions, and PSDrives that are built-in to PowerShell as well as any which are made by your PowerShell profile.
🌐
Hosting Ultra So
hostingultraso.com › help › windows › control-access-scope-variables-other-items-windows-powershell
Control Access and Scope of Variables and Other Items in Windows PowerShell | Windows PowerShell, Windows Server | HostingUltraso.com
Problem You want to control how you define (or interact with) the visibility of variables, aliases, functions, and drives. Solution PowerShell offers several ways to access variables. To create a variable with a specific scope, supply that scope before the variable name: $SCOPE:variable = value To ...
🌐
PowerShell Forums
forums.powershell.org › powershell help
Powershell Module - variable Scopes - general advice - PowerShell Help - PowerShell Forums
October 19, 2017 - Just a query regarding a module I have which contains a number of simple functions to achieve a simple task (About 8 steps). I have an inline script with the code and it works to achieve this task. I began to break it down into a ‘each function’ does exactly one job and one job only and ...
🌐
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 - And, the module does not have its own scope, although the scripts in the module, like all Windows PowerShell scripts, do have their own scope." After reading that, I contacted Joel since I'm familiar with global, script, and local scope but not module scope. My question to Joel was something like "What is this Module Scope that you Speak of?". Joel replied and said script scope in a module is module scope. A little testing confirmed that scoping variables to script scope within a module makes them shareable between the commands from that module without exposing them globally.
🌐
PowerShell Forums
forums.powershell.org › powershell help
Powershell functions and variable scope - PowerShell Help - PowerShell Forums
December 31, 2011 - by Lembasts at 2012-10-02 18:39:49 Greetings, I have several functions which need to update variables defined in the parent scope. One option is to use the prefix like global: before each variable but that looks mess…
🌐
Powershellbyexample
powershellbyexample.dev › post › variable-scopes
Scopes | PowerShell By Example
Items in the global scope are available everywhere. To define an item as global you use the global: modifier. $global:varOne = "bla" # Assign a variable in the global scope Write-Host "Variable One:" $global:varOne # Print the variable # Function to demonstrate local and global scope function MyFunc() { $global:varOne = "bla bla" $varTwo = "boo" return $varTwo } Write-Host "Variable Two:" $varTwo # Print the variable $varTwo = MyFunc # Call the function and change the variable Write-Host "Variable One:" $varOne # Print the variable Write-Host "Variable Two:" $varTwo # Print the variable