Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Path of currently executing powershell script [duplicate]

Tags:

powershell

People also ask

How do you get the path of the currently executing script in PowerShell?

To get the full path of the script we need to use the $myInvocation command. This is an automatic variable and it is only invoked when the script or the function is executed. $MyInvocation.

Where is PowerShell script located?

Using the $PSScriptRoot Variable to Get the Current Location of the PowerShell Script. If you are running PowerShell version 3 or later, an automatic variable has been introduced to store the current file or module's directory. The $PSScriptRoot variable returns the value of the directory of the running script.

Where are PS scripts stored?

By default it is located in C:\Users\<username>.

How do I get the file path in PowerShell?

Use the Get-ChildItem cmdlet in PowerShell to get the full path of the file in the current directory. Get-ChildItem returns one or more items from the specified location and using the file FullName property, it gets the full path of the file.


For PowerShell 3.0 users - following works for both modules and script files:

function Get-ScriptDirectory {
    Split-Path -parent $PSCommandPath
}

From Get-ScriptDirectory to the Rescue blog entry ...

function Get-ScriptDirectory
{
  $Invocation = (Get-Variable MyInvocation -Scope 1).Value
  Split-Path $Invocation.MyCommand.Path
}

Split-Path $MyInvocation.MyCommand.Path -Parent