The automatic variables you are using are information about the script invocation. The location from which the command to launch the script was initiated is part of the environment.
$PWD
contains information about the present working directory (nod to posix pwd
command). Specifically, $PWD.Path
.
Per the about_automatic_variables
page (or Get-Help about_automatic_variables)
, $PSScriptRoot
, $PSCommandPath
, are properties of $MyInvocation
.
See here for an example of using Split-Path -Path $($Global:$MyInvocation.MyCommand.Path)
to get the current path.
Recommend a test script:
# TestInvocationAndPWDPaths.ps1
function Test-MyInvocation {
$MyInvocation
}
function Test-PWD {
$PWD
}
'$MyInvocation from script:'
$MyInvocation
'$MyInvocation from function:'
Test-MyInvocation
'$PWD from script:'
$PWD
'$PWD from function'
Test-PWD
Has interesting results. Running this from powershell console, and from ISE, and from command prompt will show you the differences in $MyInvocation.