So I have a personal project I'm working on where I want to set the value of $Name
to the name of the script without the path or file extensions. I thought I could do this as a global variable since it needs to called a few times.
I've used the set-variable
command and used $global:Name
but nothing seems to work when I call the variable in the function. I've also used other variable names such as "foo" and it still wasn't working. Here are some example code snippets:
Set-Variable -name Name -value [IO.Path]::GetFileNameWithoutExtension($MyInvocation.ScriptName) -scope global
function test {
$Name
}
test
Pause
I've also tried:
Set-Variable -name Name -value [IO.Path]::GetFileNameWithoutExtension($MyInvocation.ScriptName) -scope global
function test {
$global:Name
}
test
Pause
And:
$global:Name = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.ScriptName)
function test {
$Name
}
test
Pause
And:
$global:Name = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.ScriptName)
function test {
$global:Name
}
test
Pause
And:
$script:Name = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.ScriptName)
function test {
$script:Name
}
test
Pause
And:
$script:Name = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.ScriptName)
function test {
$Name
}
test
Pause
I was expecting the name of the script to appear but it just returns null. Anyone know why this is happening? Any advice is much appreciated!