Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current directory of the cmdlet being executed

People also ask

How do I change the current directory in PowerShell?

Use the Set-Location Cmdlet to Change the Working Directory in PowerShell. The Set-Location cmdlet similarly sets the working directory to a specified location like its legacy counterpart cd and chdir .


Yes, that should work. But if you need to see the absolute path, this is all you need:

(Get-Item .).FullName

The reliable way to do this is just like you showed $MyInvocation.MyCommand.Path.

Using relative paths will be based on $pwd, in PowerShell, the current directory for an application, or the current working directory for a .NET API.

PowerShell v3+:

Use the automatic variable $PSScriptRoot.


The easiest method seems to be to use the following predefined variable:

 $PSScriptRoot

about_Automatic_Variables and about_Scripts both state:

In PowerShell 2.0, this variable is valid only in script modules (.psm1). Beginning in PowerShell 3.0, it is valid in all scripts.

I use it like this:

 $MyFileName = "data.txt"
 $filebase = Join-Path $PSScriptRoot $MyFileName

You can also use:

(Resolve-Path .\).Path

The part in brackets returns a PathInfo object.

(Available since PowerShell 2.0.)


Try :

(Get-Location).path

or:

($pwd).path