How to Get Current Directory in PowerShell?

In a training session, someone asked me about retrieving the correct directory in PowerShell. This is a very common requirement for PowerShell developers. In this tutorial, I will explain how to get the current directory in PowerShell using different methods with examples.

To get the current directory in PowerShell, you can use the Get-Location cmdlet. This cmdlet returns the current directory path as an object. Simply type Get-Location in your PowerShell console to display the current directory, such as C:\Users\JohnDoe.

Get Current Directory in PowerShell

Now, let me show you different methods to get the current directory in PowerShell.

Using PowerShell Get-Location Cmdlet

The best way to get the current directory in PowerShell is by using the Get-Location cmdlet. This cmdlet returns the current directory as an object, similar to the pwd command in Unix-like systems.

Syntax

Here is the syntax:

Get-Location

Example

Now, let me show you an example.

Get-Location

Path
----
C:\Users\fewli

In this example, running Get-Location returns C:\Users\fewli, which is the current directory.

I executed the above PowerShell cmdlet, and you can see the output in the screenshot below:

powershell get current directory

Check out How to Change Directory in PowerShell?

Store the Current Directory in a Variable

Sometimes, you may need to store the current directory in a variable for later use. You can easily achieve this by assigning the output of Get-Location to a variable.

Example

Here is an example.

$currentDir = Get-Location
Write-Output "The current directory is $($currentDir.Path)"

This script stores the current directory in the $currentDir variable and then outputs it.

Here is the exact output in the screenshot below:

get current directory in PowerShell

Using $PWD Automatic Variable

PowerShell provides an automatic variable $PWD that holds the current directory. This variable can be a quick and efficient way to access the current directory without invoking a cmdlet.

Example

Here is an example.

$PWD

Path
----
C:\Users\fewli

Here, $PWD directly gives you the current directory path.

Here is the exact output in the screenshot below:

How to get current directory in PowerShell

Check out How To Get Newest File In Directory In PowerShell?

Retrieve the Directory of the Executing Script

If you need to get the directory where your script is located, you can use the $MyInvocation automatic variable. This is particularly useful when your scripts are dependent on relative paths.

Example

$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Write-Output "The script is located in $scriptDir"

In this example, $MyInvocation.MyCommand.Path provides the full path of the script, and Split-Path -Parent extracts the directory.

Here is the exact output in the screenshot below:

Retrieve the Directory of the Executing Script in PowerShell

Combine Methods for Advanced Use Cases

Let me show you another advanced use case for this.

Suppose you could change the directory and then return to the original directory after performing some tasks. Then, you can write the PowerShell script below.

Example

# Store the current directory
$originalDir = Get-Location

# Change to a new directory
Set-Location "C:\Users\JohnDoe\Projects"

# Perform some tasks
Write-Output "Current directory: $(Get-Location)"

# Return to the original directory
Set-Location $originalDir
Write-Output "Returned to original directory: $(Get-Location)"

In this script, we store the original directory, change to a new directory, perform tasks, and then return to the original directory.

Conclusion

In this tutorial, I explained how to get the current directory in PowerShell using various methods, such as using the Get-Location, $PWD, or $MyInvocation cmdlets.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.