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.

Setting a global PowerShell variable from a function where the global variable name is a variable passed to the function - Stack Overflow
Global vs Script scope
PowerShell — Understanding Scope - by Rod Trent - myITforum
Creating New Global Variables using New-Variable
Videos
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.
You can use the Set-Variable cmdlet. Passing $global:var3 sends the value of $var3, which is not what you want. You want to send the name.
$global:var1 = $null
function foo (
b, $varName)
{
Set-Variable -Name $varName -Value (
b) -Scope Global
}
foo 1 2 var1
This is not very good programming practice, though. Below would be much more straightforward, and less likely to introduce bugs later:
$global:var1 = $null
function ComputeNewValue (
b)
{
b
}
$global:var1 = ComputeNewValue 1 2
As simple as:
$A="1"
function changeA2 () { $global:A="0"}
changeA2
$A
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?
EDIT: Solved. I figured it out myself actually. I add -Scope "Global". See example:
New-Variable -Name $newVar -Value $newVal -Force -Scope "Global"
The result ends up being:
$MyDomain www.myDomain.net $global:MyDomain www.myDomain.net
Basically, a local AND global variable end up getting set.
***Original Post***
I have something like this outside of any function:
$global:MyDomain=""
The rest is inside a function.
I then read a file called Info.txt. The following represents a single line as an example:
global.MyDomain:www.myDomain.net
I have code which will split the previous line and get the values:
$myVar global.MyDomain $myVal www.myDomain.net
The period found in $myVar is replaced with a colon:
$myVar=$myVar.Replace(".",":")
$myVar
global:MyDomainThe idea is to create a new variable, based on the contents of $myVar. Thus, the new variable would be called $global:MyDomain, where it would be equal to www.myDomain.net. I use this code:
New-Variable=-Name $newVar -Value $newVal -Force
Here's the problem:
If I take $global. out of the line, thus making it a local variable, it will work just fine. As a global variable, it will not. It has to do somehow with New-Variable.
Can I create a new global variable using New-Variable or is there another way to do so which will work for global variables?