Get Current Directory in PowerShell

Get Current Directory in PowerShell

Using Get-Location Cmdlet

Use the Get-Location cmdlet to get the path of the current working directory in PowerShell.

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.

Now, let’s run the Get-Location cmdlet to get the location of the current working directory and see if it has been changed.

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.

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 $CurrentDirectoryin code above.

Using $pwd Command

Use the $pwd command to get the path of the current working directory in PowerShell.

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.

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:

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:

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.

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.

Using System.Environment Class

Use the System.Environment class to access its CurrentDirectory property to retrieve the current working directory in PowerShell.

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.

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.

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.

Was this post helpful?

Related Posts

  • Check if Service is running in PowerShell
    05 January

    Check if Service is Running in PowerShell

    Table of ContentsUsing Get-Service CmdletUse Get-Service Cmdlet with if-else BlockUsing Get-SPServiceApplication Cmdlet Using Get-Service Cmdlet Use Get-Service with the Where-Object cmdlet to check if a particular service is running on our local machine. [crayon-63b6c8b2d460f391204203/] [crayon-63b6c8b2d461a951684062/] If you need to put if condition, then head over to this section . We used the Get-Service cmdlet to […]

  • Run reg file in PowerShell
    05 January

    Run .reg File Using PowerShell

    Table of ContentsUsing Invoke-Item CmdletUsing Start-Process CmdletUsing iex OperatorUsing . OperatorUsing reg import Command Using Invoke-Item Cmdlet Use the Invoke-Item cmdlet to run the .reg file using PowerShell. [crayon-63b6c8b2bff38971235319/] For the above command, Invoke-Item performed its default action, which means it allowed us to execute a .reg file. We can also use it to run […]

  • PowerShell restart Computer
    05 January

    PowerShell – Restart Computer

    Table of ContentsUsing Restart-Computer cmdletUse Restart-Computer cmdlet for Local ComputersUse Restart-Computer cmdlet for Remote ComputerUsing Shutdown CommandUse Shutdown Command for Local ComputersUse Shutdown Command for Remote ComputersExamplesExample 1: Restart local computerExample 2: Restart multiple computersExample 3: Restart remote computer with wait, timeout, delay and for parameters. Using Restart-Computer cmdlet Use the Restart-Computer cmdlet to restart […]

  • Check if Variable is null in PowerShell
    04 January

    PowerShell – Check If Variable Is Null

    Table of ContentsUsing $null VariableUsing Conditional StatementUsing IsNullorEmpty() MethodUsing IsNullOrWhiteSpace() MethodUsing Null-Coalescing Operators Using $null Variable Use the $null variable with the -eq parameter to check whether a variable is equal to $null. [crayon-63b6c8b2d4ae4133639714/] [crayon-63b6c8b2d4ae7618669116/] The above code returned a Boolean value True which means a variable is equal to null. What is null? null […]

  • PowerShell get filename for Path
    03 January

    PowerShell – Get Filename from Path

    Table of ContentsPowerShell – Get Filename From Specified PathUsing Split-Path CmdletUsing Get-Item CmdletUsing Get-ChildItem CmdletUsing GetFileName() MethodGetting Multiple File NamesWith ExtensionsWithout Extension PowerShell – Get Filename From Specified Path The location of a file on a system can be determined by its path. In PowerShell, there are several ways to get filename from path. Firstly, […]

  • Count files in folder in PowerShell
    01 January

    PowerShell – Count Files in Folders

    Table of ContentsCount Files in Folders in PowerShellCount Files in multiple Folders in PowerShellCount Files in Folders and Subfolders in PowerShellCount only folders inside folder in PowerShellCount Files by Extension in Folder in PowerShell Count Files in Folders in PowerShell We can use the Measure-Object cmdlet in different ways to meet our project requirements. However, […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.