Wednesday, 28 May 2014

PowerShell pre 3.0: Where does this script live?

Before PowerShell 3.0's $PSCommandPath which holds the full path to the file and $PSScriptRoot which contains the path to the file's directory we had to hand-craft it.

$private:PSCommandPath = $MyInvocation.MyCommand.Path
$private:PSScriptRoot = (Split-Path -Parent $PSCommandPath)

# echo $private:PSCommandPath 
# echo $private:PSScriptRoot
# Call MyOtherScript.ps1 which is in the same directory as this & (Join-Path -Path $private:PSScriptRoot -ChildPath "MyOtherScript.ps1")

See also: Which version of PowerShell am I running?