Whenever I need to reference a common module or script, I like to use paths relative to the current script file, that way, my script can always find other scripts in the library.
So, what is the best, standard way of determining the directory of the current script? Currently, I'm doing:
$MyDir = [System.IO.Path]::GetDirectoryName($myInvocation.MyCommand.Definition)
# This is an automatic variable set to the current file's/module's directory $PSScriptRoot
Prior to PowerShell 3, there was not a better way than querying the
MyInvocation.MyCommand.Definition property for general scripts. I had the
following line at the top of essentially every powershell script I had:
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition