Table of Contents
Using Get-Location
Cmdlet
Use the Get-Location
cmdlet to get the path of the current working directory in PowerShell.
1 2 3 |
Get-Location |
1 2 3 4 5 |
Path ------ C:\Users\DELL |
As we can see above, our current working directory is C:\Users\DELL, which means we are currently working in the DELL
folder within the Users
folder on the C
drive of the Windows operating system.
We can also set different directory paths in PowerShell using the Set-Location
cmdlet. Right now, we are in the C:\users\DELL directory.
1 2 3 |
Set-Location C:\Intel\project |
Now, let’s run the Get-Location
cmdlet to get the location of the current working directory and see if it has been changed.
1 2 3 |
Get-Location |
1 2 3 4 5 |
Path ---- C:\Intel\project |
We have successfully changed the current working directory from C:\users\DELL to C:\Intel\project. If there is a situation to write the current working directory with a custom message, we can use variables to achieve the goal.
1 2 3 4 |
$CurrentDirectory = Get-Location Write-Host "Our current Working Directory is $CurrentDirectory" |
1 2 3 |
Our current Working Directory is C:\Intel\project |
In this code, Get-Location
assigned the current directory to a variable called $CurrentDirectory
. Using the Write-Host
cmdlet, we can print custom messages on the console.
So, we used the Write-Host
to display the current directory’s path on a PowerShell terminal with a custom message. Note: In PowerShell, we can declare a variable using the prefix $
as we declared $CurrentDirectory
in code above.
Using $pwd
Command
Use the $pwd
command to get the path of the current working directory in PowerShell.
1 2 3 |
$pwd |
1 2 3 4 5 |
Path ---- C:\Intel\project |
Here, we used $pwd
to get the current working directory. The $pwd
contains a path object retrieving the current directory’s full path location. In PowerShell, this command is an alias for the Get-Location
cmdlet.
A $pwd
is an automatic variable, one of the System.Management.Automation.PathInfo
types. All the members of PathInfo
are accessible through this variable. If you want to learn more about it, check out this PathInfo.
Using Resolve-Path
Cmdlet [ Relative Path]
Use the Resolve-Path
cmdlet to obtain relative paths to files or subfolders from the current directory in PowerShell.
1 2 3 4 |
$relativePath = Get-Item \Intel\project\javaScript.txt | Resolve-Path -Relative $relativePath |
1 2 3 |
.\project\javaScript.txt |
In PowerShell, we may need to obtain relative paths to files or subfolders from the current directory. To accomplish this, we used the Get-Item
cmdlet to get the items in the \Intel\project.txt\javaScript.txt
. Next, we used the Resolve-Path
cmdlet with the -Relative
parameter to get a relative path and assigned it to a variable named $relativePath
.
In the script above, we could see that we were inside the folder C:/Intel
, which contained a subfolder project
, which had an item or a file called javaScript.txt
.
We can observe that the relative path \project\javaScript.txt is returned from the current directory of PowerShell instead of the absolute path C:\Intel\project\javaScript.txt.
Now, what are relative and absolute paths? In a file system, a path is a string of characters that specifies the location of a file or directory. There are two kinds of paths: absolute paths & relative paths.
An absolute path specifies the exact location of a file or directory, starting from the file system’s root. For example, in a Windows system, the absolute path of the C:\Users\DELL\Documents directory might look like this:
1 2 3 |
C:\Users\DELL\Documents |
An absolute path always begins with the root directory (C:\ in this case) and specifies the complete hierarchy of directories down to the target file or directory.
On the other hand, a relative path specifies the file’s location or directory relative to the current working directory. The current working directory is the directory the user is in, where any new files or directories will be created by default.
For instance, if the current working directory is C:\Users\DELL\Documents and you want to specify the location of the Downloads
directory, you could use a relative path like this:
1 2 3 |
..\Downloads |
This relative path specifies that the Downloads directory is located one level up from the current working directory (..
stands for parent directory
). In the following section, we will learn how to get the parent directory.
Using Split-Path
Cmdlet [ Get Parent working directory]
Use the Split-Path
cmdlet to split the specified path and extract the parent working directory.
1 2 3 4 |
$CurrentDirectory = Get-Location Split-Path -Path $CurrentDirectory -Parent |
1 2 3 |
C:\Intel |
First, we have assigned the current location to the variable called$CurrentDirectory
. Second, we used Split-Path
along with parameters -Path
and -Parent
to fetch the parent working directory from the specified path.
The -Path
parameter is used to accept the path string that we want to split. Here the directory’s full path C:\Intel\project is passed to it. (Note we can also pass more than one path string if the -path
parameter name is used).
The -Parent
parameter returns the parent location (without the filename) of the specified path. As we can see above, C:\Intel is returned, which is the parent directory. Note that -Parent
is the default split
location parameter, which means if we miss this -Parent
parameter, we will still get the parent directory in the result.
You can refer to this documentation for more information on the Split-Path
cmdlet.
Further reading:
Using System.Environment
Class
Use the System.Environment
class to access its CurrentDirectory
property to retrieve the current working directory in PowerShell.
1 2 3 |
[System.Environment]::CurrentDirectory |
1 2 3 |
C:\Users\DELL |
Here, we used the CurrentDirectory
property of the System.Environment
.NET class to get the current working directory. Here’s one thing to notice it returned C:\Users\DELL as the current directory path even though the working location was C:\Intel.
In PowerShell, we can create multiple runspaces per process. The current directory of each runspace is different. This is not the same as [System.Environment]::CurrentDirectory
.
The Environment::CurrentDirectory
refers to the directory in which the process is currently working. It differs from the PowerShell location, which is specific to the runspace where the pipelining is running.
Note: Avoid using [Environment]::CurrentDirectory
and keep it simple and use Get-Location
or $PWD
to get the path of the current working directory.
Using $PSScriptRoot
Variable [Get Current directory of PowerShell Script ]
Use the $PSScriptRoot
variable to determine the current directory in which a script file (.ps1
) is located.
1 2 3 4 5 |
$strDateTime = "2022-12-30" [DateTime]$strDateTime Write-Host 'Our Current working directory is:' $PSScriptRoot |
The above script file is located at C:\Intel\project\ConvertString-toDate.ps1 . Note that the script file contained an automatic variable, $PSScriptRoot
, invoked when the script ran. Now, let’s run the above script file in PowerShell as follows.
1 2 3 |
C:\Intel\project\ConvertString-toDate.ps1 |
1 2 3 4 |
Friday, December 30, 2022 12:00:00 AM Our Current working directory is: C:\Intel\project |
When we ran the above ConvertString-to-Date.ps1
script file, it converted a string to DateTime
format, and the automatic variable $PSScriptRoot
retrieved the script’s current directory path and displayed it.
That’s all about how to get current directory in PowerShell.