-
-
Save aniston/c3a7d2f1cc2e1a73c62d5d95a5969d84 to your computer and use it in GitHub Desktop.
Use different techniques to determine a PowerShell script's directory: try `$PSScriptRoot`, `$MyInvocation.MyCommand.Path`, `$ExecutionContext.SessionState.Module.Path` and `$PWD`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-ScriptPath { | |
$scriptDir = Get-Variable PSScriptRoot -ErrorAction SilentlyContinue | ForEach-Object { $_.Value } | |
if (!$scriptDir) { | |
if ($MyInvocation.MyCommand.Path) { | |
$scriptDir = Split-Path $MyInvocation.MyCommand.Path -Parent | |
} | |
} | |
if (!$scriptDir) { | |
if ($ExecutionContext.SessionState.Module.Path) { | |
$scriptDir = Split-Path (Split-Path $ExecutionContext.SessionState.Module.Path) | |
} | |
} | |
if (!$scriptDir) { | |
$scriptDir = $PWD | |
} | |
return $scriptDir | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment