I want to be able to tell what path my executing script was run from.
This will often not be $pwd.
I need to call other scripts that are in a folder structure relative to my script and while I could hard code the paths, that's both distasteful and a bit of a pain in the neck when trying to promote from "dev" to "test" to "production".
In this case, first, we need the current script's path, and from it, we use dirname to get the directory path of the script file. Once we have that, we cd into the folder and print the working directory. To get the full or absolute path, we attach the basename of the script file to the directory path or $DIR_PATH.
How do I find out the current directory location and shell script directory location in Bash running on Linux or Unix like operating systems? basename command – Display filename portion of pathname. dirname command – Display directory portion of pathname.
The ubiquitous script originally posted by Jeffrey Snover of the PowerShell team (given in Skyler's answer) and the variations posted by Keith Cedirc, and EBGreen, all suffer from a serious drawback--whether the code reports what you expect depends on where you call it!
My code below overcomes this problem by simply referencing script scope instead of parent scope:
function Get-ScriptDirectory { Split-Path $script:MyInvocation.MyCommand.Path }
To illustrate the problem, I created a test vehicle that evalutes the target expression in four different ways. (The bracketed terms are the keys to the following result table.)
The last two columns show the result of using script scope (i.e. $script:) or with parent scope (with -scope 1). A result of "script" means that the invocation correctly reported the location of the script. The "module" result means the invocation reported the location of the module containing the function rather than the script that called the function; this indicates a drawback of both functions that you cannot put the function in a module.
Setting the module issue aside the remarkable observation from the table is that using the parent scope approach fails most of the time (in fact, twice as often as it succeeds).
Finally, here is the test vehicle:
function DoubleNested() { "=== DOUBLE NESTED ===" NestCall } function NestCall() { "=== NESTED ===" "top level:" Split-Path $script:MyInvocation.MyCommand.Path #$foo = (Get-Variable MyInvocation -Scope 1).Value #Split-Path $foo.MyCommand.Path "immediate func call" Get-ScriptDirectory1 "dot-source call" Get-ScriptDirectory2 "module call" Get-ScriptDirectory3 } function Get-ScriptDirectory1 { Split-Path $script:MyInvocation.MyCommand.Path # $Invocation = (Get-Variable MyInvocation -Scope 1).Value # Split-Path $Invocation.MyCommand.Path } . .\ScriptDirFinder.ps1 Import-Module ScriptDirFinder -force "top level:" Split-Path $script:MyInvocation.MyCommand.Path #$foo = (Get-Variable MyInvocation -Scope 1).Value #Split-Path $foo.MyCommand.Path "immediate func call" Get-ScriptDirectory1 "dot-source call" Get-ScriptDirectory2 "module call" Get-ScriptDirectory3 NestCall DoubleNested
Contents of ScriptDirFinder.ps1:
function Get-ScriptDirectory2 { Split-Path $script:MyInvocation.MyCommand.Path # $Invocation = (Get-Variable MyInvocation -Scope 1).Value # Split-Path $Invocation.MyCommand.Path }
Contents of ScriptDirFinder.psm1:
function Get-ScriptDirectory3 { Split-Path $script:MyInvocation.MyCommand.Path # $Invocation = (Get-Variable MyInvocation -Scope 1).Value # Split-Path $Invocation.MyCommand.Path }
I am not familiar with what was introduced in PowerShell 2, but it could very well be that script scope did not exist in PowerShell 1, at the time Jeffrey Snover published his example.
I was surprised when, though I found his code example proliferated far and wide on the web, it failed immediately when I tried it! But that was because I used it differently than Snover's example (I called it not at script-top but from inside another function (my "nested twice" example).)
2011.09.12 Update
You can read about this with other tips and tricks on modules in my just-published article on Simple-Talk.com: Further Down the Rabbit Hole: PowerShell Modules and Encapsulation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With