Skip to content

Instantly share code, notes, and snippets.

@aniston
aniston / Get-ScriptPath.ps1
Last active March 5, 2023 07:36 — forked from glombard/Get-ScriptPath.ps1
Use different techniques to determine a PowerShell script's directory: try `$PSScriptRoot`, `$MyInvocation.MyCommand.Path`, `$ExecutionContext.SessionState.Module.Path` and `$PWD`.
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)
@aniston
aniston / Get-ScriptPath.ps1
Created February 8, 2017 21:21 — forked from glombard/Get-ScriptPath.ps1
Use different techniques to determine a PowerShell script's directory: try `$PSScriptRoot`, `$MyInvocation.MyCommand.Path`, `$ExecutionContext.SessionState.Module.Path` and `$PWD`.
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)