Created
May 2, 2014 10:45
-
-
Save glombard/1ae65c7c6dfd0a19848c 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 hidden or 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 { | |
$scritDir = 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 | |
} |
Only $PWD.Path works for me.
there is a typo on line 2, it should be $scriptDir not $scritDir
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think you made a small typo on de second line. I should be " $Scriptdir = ....... "