Skip to main content

Find Your Script’s Location in PowerShell

Problem

You want to know the location of the currently running script.

Solution

To determine the location of the currently executing script, use this function:

function GetScriptPath

{

SplitPath $myInvocation.ScriptName

}

Discussion

Once we know the full path to a script, the SplitPath cmdlet makes it easy to determine its location. Its sibling, the JoinPath cmdlet, makes it easy to form new paths from their components as well.

By accessing the $myInvocation.ScriptName variable in a function, we drastically simplify the logic it takes to determine the location of the currently running script.

Help Category