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

How to Get Current Directory Full Path in PowerShell?

Do you want to get current directory of running script in PowerShell? In this blog post, I have discussed using PowerShell to get current directory full path, current directory of script file with examples.

Get-Location cmdlet in PowerShell get current directory full path similar PowerShell pwd which print current directory.

There are different ways in PowerShell to get current directory path. We will go step by step on each of the way in PowerShell to get current directory.

PowerShell Current Directory Path

PowerShell $pwd – Print Current Directory

$pwd

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

PowerShell pwd is an alias of Get-Location cmdlet in PowerShell. $pwd automatic variable is type of System.Management.Automation.PathInfo , it 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 windows operating system. It prints the full pathname of the current working directory.

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

Set-Location C:\Users\ShellAdmin

Now run Get-Location cmdlet and see results

Get-Location

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

PowerShell Current Directory Variable

In PowerShell, get current directory to variable using below command

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

Get-Location PowerShell command assign current directory to variable curDir. Write-Host cmdlet will print current directory path on PowerShell Terminal.

Get Parent directory Path in PowerShell

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

If you want to get parent working directory , you can get directory using 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.

Second command returns 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

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

Cool Tip: Learn how to get 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 current directory path even working location is set to D:\PowerShell

PowerShell Get Current Directory of Script File

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

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

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

Script file contains $PSScriptRoot automatic variable and invoke 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 get current directory path of running script and print it.

Output of 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 print string to date, and get script directory path in PowerShell.

Conclusion

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

PowerShell Get-Location cmdlet get current working location information. Using pwd and Get-Location cmdlet in PowerShell, you can get current folder path, get script directory path, print current directory. Set-Location cmdlet used in article, to set working directory.

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

Leave a Comment