How to Get the Current Directory or Script Path in PowerShell?
As a professional working with PowerShell, I frequently receive questions to get the current directory from the script. PowerShell, Microsoft’s versatile scripting language, is integrated deeply into the Windows operating system. This makes it incredibly potent for handling file and directory manipulation tasks, making it important to know the current path you are working from. So, In this guide, we’ll walk you through the various ways to get the current directory, script directory, and script path using PowerShell. Whether you want the path of the existing script or the full path of the current location, we’ve got you covered.
Table of contents
- Understanding the Current Directory in PowerShell
- How to get the current directory in PowerShell?
- Method 1: Using the Get-Location Cmdlet
- Method 2: Getting the script directory in PowerShell using $PSScriptRoot variable
- Method 3: Getting the script path in PowerShell
- Method 4: Using the $PWD Variable
- Method 5: Using the [Environment]::CurrentDirectory Property
- Method 6: Get the current directory path using Get-Item cmdlet
- Common mistakes to avoid when working with directories in PowerShell
- Conclusion and final thoughts
Understanding the Current Directory in PowerShell
In PowerShell, the current directory refers to the directory that you are currently working in. This is important because many cmdlets and scripts work with files and directories relative to the current directory. For example, if you are in the “C:\Users\Thomas\Desktop” directory, and you run a script that accesses a file in the Documents directory, the script will look for the file in C:\Users\Thomas\Desktop\Documents instead of C:\Users\Thomas\Documents. One can easily create, move, rename, and delete directories in PowerShell. But before you can work with directories effectively, you need to know where you currently are. That’s where the “Get Current Directory” operation steps into the spotlight.
The current directory is not the same as the PowerShell installation directory or the user profile directory. You can change the current directory using the Set-Location cmdlet or navigate to a new directory using the cd command.
How to get the current directory in PowerShell?
Whether you’re dealing with files, making configuration changes, or working with scripts, being cognizant of your current directory is critical. The current directory, or working directory, is the directory (folder) in which your PowerShell environment is currently operating. It defines the point from which relative paths originate and plays a crucial role in file management tasks.
Method 1: Using the Get-Location Cmdlet
The most direct way to obtain your current directory in PowerShell is by employing the Get-Location
cmdlet. This cmdlet retrieves the current working location, i.e., the path of the directory you’re in. This cmdlet returns the Path of the current directory, including the path and the provider (file system or registry). To use it, type “Get-Location” and press Enter. The system will display the full path of the current directory in the PowerShell prompt.
The syntax for the Get-Location cmdlet goes like this:
#Syntax 1
Get-Location
[-PSProvider <String[]>]
[-PSDrive <String[]>]
[<CommonParameters>]
#Syntax 2
Get-Location
[-Stack]
[-StackName <String[]>]
[<CommonParameters>]
Here is the list of nifty parameters of this cmdlet.
Parameter | Description | Example |
---|---|---|
-PSDrive | Retrieves the current location from the specified PowerShell drive. E.g., PowerShell Registry Provider. | Get-Location -PSDrive HKCU |
-PSProvider | Retrieves the current location for the specified provider. Providers can include the FileSystem, Registry, Certificate stores, etc. If the provider supports multiple drives, this cmdlet returns the location of the most recently accessed drive. | Get-Location -PSProvider FileSystem |
-Stack | Displays the location default stack. PowerShell maintains a stack of locations you can push to and pop from using Push-Location cmdlet and Pop-Location cmdlet. This parameter shows the current stack contents. | Get-Location -Stack |
-StackName | The Stackname parameter displays the location from a named location stack. You can have multiple named location stacks. | Get-Location -StackName myStack |
It can move between PowerShell drives, different drives, Windows directories, etc. Here’s an example to get the current directory path using the Get-Location command:
PS C:\Users\Thomas> Get-Location
Path
----
C:\Users\Thomas
This example shows that the current Windows directory is C:\Users\Thomas. You can also use the alias “pwd” to get the current directory. You use the pwd (short for “print working directory”) cmdlet to display the current directory in the PowerShell console. To use it, type “pwd” and press Enter. The full path of the current directory will be displayed.
PS C:\Users\Thomas> pwd
Path
----
C:\Users\Thomas
When you run this command, PowerShell returns the path of your current directory. This serves as your springboard, the starting point from which all relative paths are defined.
Method 2: Getting the script directory in PowerShell using $PSScriptRoot variable
The script directory is the directory where the currently running script is located. This is useful for accessing other files or scripts in the same directory as the script. To get the script directory in PowerShell, you can use the $PSScriptRoot variable. This variable contains the full path of the directory where the script is located. Here’s an example: Say you have a PowerShell script file called “CopyFiles.ps1” with the below content:
Echo $PSScriptRoot
Executing the script retrieves the current location where the script is stored.
PS C:\Users\Thomas\Desktop> .\CopyFiles.ps1
C:\Users\Thomas\Desktop
This example shows that the script directory is C:\Users\Thomas\Desktop\. Note that the $PSScriptRoot variable is only available in scripts, not in the PowerShell console or PowerShell ISE.
Method 3: Getting the script path in PowerShell
The script path is the full path of the currently running script, including the drive letter and any subdirectories. To get the script path in PowerShell, you can use the $MyInvocation variable. This variable contains information about the current script, including the path and the command line arguments. Here’s an example:
#Get Script Path
$MyInvocation.MyCommand.Path
#Get the folder of the script
Split-Path -Path $MyInvocation.MyCommand.Path -Parent
Here is what you’ll see when running the script:
PS C:\Users\Thomas\Desktop> .\MyScript.ps1
C:\Users\Thomas\Desktop\MyScript.ps1
C:\Users\Thomas\Desktop
This example shows the absolute path of the script: C:\Users\Thomas\Desktop\MyScript.ps1. Note that the $MyInvocation variable is only available in scripts, not in the PowerShell console.
Method 4: Using the $PWD Variable
Another way to get the current directory in PowerShell is by using the $PWD variable. This variable holds the current working directory as a PowerShell object. To display the current directory using this method, type “$PWD” and press Enter. The full path of the current directory will be displayed.
$pwd | Select -ExpandProperty Path
Method 5: Using the [Environment]::CurrentDirectory Property
Finally, you can also get the current directory in PowerShell by using the [Environment]::CurrentDirectory property. This property returns the path of the current directory as a string. To display the current directory using this method, type “[Environment]::CurrentDirectory” and press Enter. The full path of the current directory will be displayed.
[Environment]::CurrentDirectory
Method 6: Get the current directory path using Get-Item cmdlet
You can get the current directory path using the Get-Item cmdlet using the following command:
(Get-Item .).FullName
Here, the dot (.) specifies the current directory.
Common mistakes to avoid when working with directories in PowerShell
Here are some common mistakes to avoid when working with directories in PowerShell:
- Assuming that the current directory is always the same. The current directory can be changed by scripts, cmdlets, or user input.
- Using relative paths without considering the current directory. Relative paths are resolved relative to the current directory, so changing the current directory can affect the results.
- Forgetting to handle errors when accessing files or directories. PowerShell provides many error handling mechanisms, such as the Try-Catch-Finally statement and the -ErrorAction parameter.
Conclusion and final thoughts
In conclusion, there are several ways to get the current directory in PowerShell, ranging from simple commands like pwd and Get-Location to using variables and properties. In this guide, I have explained the concept of the current directory in PowerShell and shown you how to get the current directory, script directory, and script path using PowerShell. Whether you need the full path or script directory, it’s quick and easy to get in PowerShell. Experiment with these methods to find the one that works best for your needs.
To change the location to a different path, Use the Set-Location cmdlet (E.g., Set-Location C:\Temp). More here: How to Change the current Directory in PowerShell?
To retrieve the current directory in PowerShell, you can use the PowerShell cmdlets like “Get-Location”. $currentDirectory = Get-Location
Write-Host "Current directory: $($currentDirectory.Path)"
To get the path of the current script in PowerShell, you can use the “$MyInvocation.MyCommand.Path” command.
To access a specific folder in PowerShell, you can use the “Set-Location” cmdlet or the “cd” alias, followed by the path of the folder you want to access.
In PowerShell, you can find the path of a folder using Resolve-Path for Relative Paths:$RelativePath = ".\Logs"
$FullPath = Resolve-Path $RelativePath
Write-Output $FullPath