Home » PowerShell » How to Get Current Directory Full Path in PowerShell?

How to Get Current Directory Full Path in PowerShell?

To get the current directory full path in PowerShell, use the Get-Location cmdlet. Another option is to use the PowerShell $PWD variable which represents the current working directory in PowerShell.

In this tutorial, we will explore different methods to retrieve the current directory’s fill path in PowerShell with examples.

Using PowerShell $pwd – Get Current Directory Path

$pwd (print working directory) variable in PowerShell gets the current path of the current working directory to the standard output. This variable provides access to the full path path of the current directory.

$pwd

PowerShell $pwd is an alias of the Get-Location cmdlet in PowerShell. $pwd automatic variable is a type of System.Management.Automation.PathInfo has access to PathInfo members.

Using the Get-Location cmdlet to Get Current Directory Path in PowerShell

One of the simplest ways to obtain the current directory’s full path is by using the Get-Location cmdlet in PowerShell. This command returns a PowerShell object that represents the current directory, allowing you to access its properties such as full path.

C:\Program Files>Get-Location

Get-Location cmdlet in PowerShell gets the current directory, in this case, “C:\Program Files” of the Windows operating system. It prints the full pathname of the current working directory.

Let’s set different directory paths. Set-Location cmdlet in PowerShell to set the working directory as below

Set-Location C:\Users\ShellAdmin

Now run Get-Location cmdlet to return the current folder path.

Get-Location

it now returns the current execution directory as “C:\Users\ShellAdmin“.

Using System.Environment to Get Current Directory Path in PowerShell

You can utilize [System.Environment]::CurrentDirectory to get PowerShell current directory.

PS D:\PowerShell> [System.Environment]::CurrentDirectory                                                                C:\Users\ShellAdmin
PS D:\PowerShell>                                                                                                                            

The above command returns the current directory path even if the working directory location is set to D:\PowerShell.

PowerShell Get Current Directory of Script File

To get current directory of the script file or running script, use the $PSScriptRoot automatic variable. $PSScriptRoot variable contains the full script to a path that invokes the current command.

Let’s understand get current directory of the script (ps1) file using the below example,

Example, script location: “D:\PowerShell\ConvertString-toDate.ps1”

The script file contains the $PSScriptRoot automatic variable and invokes it while running.

# Convert datetime to yyyy-mmm-dd datetime format
'{0:yyyy-MMM-dd}' -f $convertDate

# Get Current Directory path of running script
Write-Host $PSScriptRoot

$PSScriptRoot variable gets the directory where the current script is located. This variable automatically resolves to the directory containing the script file when the script is executed.

The output of the above script.

PowerShell Get Current Directory of Script File
PowerShell Get Current Directory of Script File

In the above image, when we run ConvertString-toDate.ps1 script file, it prints a string to date and gets script directory path in PowerShell.

PowerShell Current Directory Variable

In PowerShell, get the current directory to a variable using the following command.

$curDir = Get-Location
Write-Host "Current Working Directory: $curDir"

The Get-Location PowerShell command assigns current directory to variable $curDir. The write-Host cmdlet will print the current folder path on the PowerShell Terminal.

Get Parent directory Path in PowerShell

Let’s consider you having a directory structure as “C:\Backup\01-Sept\”

To get the parent working directory, you can get the directory using the below command.

$curDir = Get-Location
Split-Path -Path $curDir -Parent

In the above PowerShell script, get the current directory full path “C:\Backup\01-Sept\” and assign it to the variable $curDir.

The second command returns the parent directory path as C:\Backup using Split-Path cmdlet.

Cool Tip: Learn how to get permissions on folders and subfolders in PowerShell!

Get Script Relative Path from PowerShell Current Directory

We will continue with our above example where we have PowerShell current directory structure as “C:\Backup\01-Sept\sqlbackup.ps1”

To get the script file relative path, run the following command.

$relativePath = Get-Item Backup\01-Sept\sqlbackup.ps1 | Resolve-Path -Relative

The above command gets the relative path from the PowerShell current directory as .\01-Sept\sqlbackup.ps1

Cool Tip: Learn how to get the current directory full path in PowerShell!

Conclusion

I hope the above blog post helps you to understand using PowerShell get current directory full path in different ways.

PowerShell Get-Location cmdlet gets current working location information. Using $pwd and Get-Location cmdlet in PowerShell, you can get current folder path, get the script directory path, and print the current directory.

Set-Location cmdlet is used in the article, to set a working directory.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment