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

How to Get Current Directory Full Path in PowerShell?

The Get-Location cmdlet in PowerShell gets current directory full path similar to PowerShell pwd which prints the current directory.

In this tutorial, we will discuss using PowerShell to get current directory full path and the current directory of the script file with examples.

We will provide a detailed guide to using different ways in PowerShell to get the current directory path.

PowerShell Current Directory Path

PowerShell $pwd – Print Current Directory

$pwd

pwd (print working directory) in PowerShell get current path of the current working directory to the standard output.

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.

PowerShell Get Current Directory using Get-Location

C:\Program Files>Get-Location

Get-Location cmdlet in PowerShell get current directory, Program Files of the C drive 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 and see the results

Get-Location

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

PowerShell Current Directory Variable

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

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

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

Get Parent directory Path in PowerShell

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

If you want to get parent working directory, you can get directory using the below command

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

In the above PowerShell script, get current directory full path C:\Backup\01-Sept\ and assign it to 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 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 script file relative path

$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!

PowerShell get current directory using System.Environment

Using [System.Environment]::CurrentDirectory property to get PowerShell current directory.

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

In the above PowerShell script, System.Environment CurrentDirectory property to get current working directory. Note here, it returns the current directory path even working 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

When we run the above script file, it $PSScriptRoot variable gets current directory path of the running script and prints it.

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.

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