Unfortunately PowerShell doesn't have a conditional assignment statement like Perl ($var = (<condition>) ? 1 : 2;), but you can assign the output of an if statement to a variable:

$var = if (<condition>) { 1 } else { 2 }

Of course you could also do the "classic" approach and assign the variable directly in the respective branches:

if (<condition>) {
  $var = 1
} else {
  $var = 2
}

The second assignment doesn't supersede the first one, because only one of them is actually executed, depending on the result of the condition.

Another option (with a little more hack value) would be to calculate the values from the boolean result of the condition. Negate the boolean value, cast it to an int and add 1 to it:

$var = int) + 1
Answer from Ansgar Wiechers on Stack Overflow
🌐
Varonis
varonis.com › blog › powershell-variable-scope
PowerShell Variable Scope Guide: Using Scope in Scripts and Modules
October 13, 2022 - By setting variables using the script scope in a module, the variables are now shareable between the module’s functions. Here is an example PowerShell module file (MyModule.psm1) that contains two functions, Get-Greeting and Set-Greeting: function Get-Greeting { if ($name) { $greeting = "Hello, $name!"
🌐
4sysops
4sysops.com › home › blog › articles › the powershell variable scope
The PowerShell variable scope – 4sysops
July 28, 2023 - 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. The Script scope is one level below the Global scope. Next is the scope of a function that you define in your script, and, if you create a function within a function, you also create a new scope that sits below the other scopes.
Discussions

powershell - How can I set a variable in a conditional statement? - Stack Overflow
I have a script that checks the health of a pc by parsing through log files looking for indicators of compromise. If the script finds a certain event id it returns a normalized message. The end goa... More on stackoverflow.com
🌐 stackoverflow.com
Variable declared inside nested statement can be used outside of that statement?
Can you isolate a variable inside curly brackets so that it’s not usable outside of those brackets? I find it weird that you can still use a variable even if it’s created inside a nested statement. Is this how Powershell… More on forums.powershell.org
🌐 forums.powershell.org
0
0
August 17, 2018
PowerShell — Understanding Scope - by Rod Trent - myITforum
If multiple scripts share and modify the same global variable, debugging becomes difficult. 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 ... More on myitforum.substack.com
🌐 myitforum.substack.com
May 15, 2025
Powershell: is there a Scope Preference for Variables? - Stack Overflow
Morning guys, hopefully just a quick one. Is there a Scope Preference for Variables like there is for some other settings ($ErrorActionPreference, etc)? I'm working on a script that has a bunch of More on stackoverflow.com
🌐 stackoverflow.com
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_scopes
about_Scopes - PowerShell | Microsoft Learn
January 16, 2026 - Using scope modifiers, you can declare variables, aliases, functions, and PowerShell drives for a scope outside of the current scope. An item that you created within a scope can be changed only in the scope in which it was created, unless you explicitly specify a different scope. When code running in a runspace references an item, PowerShell searches the scope hierarchy, starting with the current scope and proceeding through each parent scope. If ...
🌐
Adam the Automator
adamtheautomator.com › powershell-scopes
PowerShell Scopes: Understanding Variable Scope
January 27, 2026 - Once the variables are set in the preferred scope, you’d then reference them the same way. Also, notice that you can exclude the scope preface if the defined scope is the local scope. PS> $global:a = 'foo' PS> $global:a foo PS> $a foo · PowerShell has a handy construct called scriptblocks.
🌐
Microsoft Learn
learn.microsoft.com › en-us › powershell › module › microsoft.powershell.core › about › about_variables
about_Variables - PowerShell | Microsoft Learn
By default, variables are only available in the scope in which they're created. For example, a variable that you create in a function is available only within the function. A variable that you create in a script is available only within the script.
🌐
PowerShell Forums
forums.powershell.org › powershell help
Variable declared inside nested statement can be used outside of that statement? - PowerShell Help - PowerShell Forums
August 17, 2018 - Can you isolate a variable inside curly brackets so that it’s not usable outside of those brackets? I find it weird that you can still use a variable even if it’s created inside a nested statement. Is this how Powershell works? $myNumber=1 $myOtherNumber=2 switch($myNumber) { (1) { if($myOtherNumber -eq 2){[byte[]]$switchScope=12,34,56} } } Get-Variable -Scope local # $switchScope IS LISTED $switchScope # VALUES 12,34,56 ARE DISPLAYED # WHY IS $switchScope NOT $null HERE?
Find elsewhere
🌐
SS64
ss64.com › ps › syntax-scopes.html
Understanding Scopes in PowerShell
You can place variables, aliases, functions, or PowerShell drives in one or more scopes. An item that you created within a scope can be changed only in the scope in which it was created, unless you explicitly specify a different scope. If you create an item in a scope, and the item shares its ...
🌐
Substack
myitforum.substack.com › p › powershell-understanding-scope
PowerShell — Understanding Scope - by Rod Trent - myITforum
May 15, 2025 - 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. If the variable is not found in any of these scopes, PowerShell generates an error.
Top answer
1 of 1
3

Is there a Scope Preference for Variables like there is for some other settings ($ErrorActionPreference, etc)?

No, scoping behavior is part of the language's core runtime semantics and is not configurable.

I'm working on a script that has a bunch of functions that call on information created in each other and I'm just looking to avoid writing $Script: or $Global: in front of each variable everytime

You don't need it everytime - you only need the scope modifier when you're writing to a parent scope.

Resolution of variables for reading will fall back through parent scopes and eventually the global scope, until a matching variable is found:

# variable defined at script scope, functions defined in here will fall back to resolving this when `$Config` is referenced
$Config = @{
  'setting' = 'initialValue'
}

function Update-Config {
  # Scope modifier is only necessary when "writing up" through the scope stack
  $script:Config = @{
    setting = 'updatedValue'
  }
}

function Do-Stuff {
  $setting = $Config['setting'] # no need to use script: here

  Write-Host "About to do something with '$setting'"
}

Do-Stuff
Update-Config
Do-Stuff

Executing the above in a script file will print:

About to do something with 'initialValue'
About to do something with 'updatedValue'

Note that for functions bound to a module, the script: scope is shared across the module - writing to $script:Variable in one module function will cause resolution of non-local $Variable to resolve correctly in any other function in the same module

For more information about scoped variable resolution, consult the about_Scopes help file

🌐
Medium
medium.com › design-bootcamp › powershell-a-powerful-tool-unit-4-variable-scope-and-loops-81b41dd5ae76
PowerShell: A Powerful Tool (Unit 4: Variable Scope and Loops) | by Er. Utkarsh Malpani | Bootcamp | Medium
January 16, 2023 - Variables are identified by referring it using a name or a number. A flow control statement specifies the block of code to be executed based on a condition. ... In an “if” statement, a condition is tested.
🌐
Stack Overflow
stackoverflow.com › questions › 32355820 › declaring-variable-in-if-statement
powershell - Declaring variable in IF statement - Stack Overflow
Why won't this work? if (([datetime] $a = Date),$a.DayOfWeek -ne 'Wednesday'){ Write-Host 1 Exit }ElseIf ($a.hour -ne 9){ Write-Host 2 Exit } Do stuff... but this will if (($conn...
🌐
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?

🌐
clan8blog
clan8blog.wordpress.com › powershell-variable-scope
PowerShell Variable Scope - clan8blog - WordPress.com
November 6, 2013 - Don’t forget point 2 if it’s not local scope then it’s a read only variable but read ahead to point 8 before you draw any conclusions 🙂 ( this is a bit like Bill and Teds excellent Adventure – just not as interesting) When assigning a value to a variable PowerShell will search the local scope to see if it can find the variable already and if id does then it assigns the value if it can’t find one then it assumes that you want to create a new local scope variable – if you want to update the value in a child scope, e.g.
🌐
PowerShell Forums
forums.powershell.org › powershell help
When to place variables inside\outside of functions? - PowerShell Help - PowerShell Forums
December 29, 2020 - Hey all, I am relatively new to PowerShell and am trying to learn to use functions. What confuses me is variable scope. My brain constantly wrestles with the question of where to place variables. The same issue pops up with Scriptblocks as well to be honest. I’ve read the MS documentation ...
🌐
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
🌐
Davemason
davemason.me › 2021 › 11 › 30 › powershell-variables-and-scope
PowerShell Variables and Scope - It's All Just Electrons
November 29, 2021 - By default, if you attempt to reference a variable that is not in scope, PowerShell will return a value of 0 (zero) or $Null, depending on type.