Path of currently executing powershell script [duplicate]

Tag: powershell Author: Dsky001 Date: 2009-07-09

This question already has an answer here:

How do I do this in PowerShell. In a batch file I would do: %~d0%~p0

By the way, you could shorten that to %~dp0

Other Answer1

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

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


See also the answer at the duplicate:

Other Answer2

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

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

Other Answer3

split-path $SCRIPT:MyInvocation.MyCommand.Path -parent


Other Answer4

In powershell 2.0

split-path $pwd


Wroking directory is not the same as scripts directory.