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-LocationExample
Now, let me show you an example.
Get-Location
Path
----
C:\Users\fewliIn 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:

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:

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\fewliHere, $PWD directly gives you the current directory path.
Here is the exact output in the screenshot below:

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:

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:
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.