Loading...
X

How to find out the current directory in the Windows command line. Analog of pwd in PowerShell

What is the current and working directory in Windows

The current directory or working directory is a folder relative to which the names of files whose path is specified relative or not specified at all are determined.

Let's say the current directory is the folder “C:\Users\MiAl”. Therefore, if I create a file test.txt without specifying an absolute path, this file will be located at C:\Users\MiAl\test.txt.

The concept of the current (working) directory is most often used when working in the console. For example, when executing a command, we specified that its output should be saved to the log.txt file (without specifying an absolute path). We need to know the current directory to find the log.txt file.

Of course, applications with a graphical interface can also operate with the concept of the current/working directory. Moreover, any running process has its own working directory – at the time of launch, this is the folder in which the executable file or script is located. But we don't need to go into that now.

When working in the console, a user might say “I'm in the C:\Users\MiAl folder” meaning the current directory. It's intuitively clear what this means, but technically, the “current directory” is just the value of the $PWD variable (this is true for both Linux and Windows). When changing the current directory, the user doesn't actually move anywhere – only the value of the $PWD variable changes.

How to view the current (working) directory in PowerShell

To display the current directory, use the following cmdlet:

Get-Location

This cmdlet has an alias that you can also use to view the working directory:

pwd

As already mentioned, information about the current working directory is stored in the $PWD variable, so to view the working directory, it is enough to display the value of this variable:

$PWD

PowerShell has another alias for Get-Location:

gl

Analog of pwd for PowerShell

If we compare the output of pwd in Linux and Get-Location, we will see that the output is different:

pwd

That is, in Linux, the pwd command only displays the value of the current directory, and in Windows, a table is displayed.

To get only the output of the current folder without showing the table, use the following command:

Get-Location | Format-Table -hide

How to change the current working directory in PowerShell

To change the current working directory, use the Set-Location cmdlet, for example:

Set-Location Z:
PS Z:\> pwd

Path
----
Z:\

You can also use the cd command, which is an alias for Set-Location in PowerShell.

See also: An analogue of cd and chdir in PowerShell. How to change the current working folder in PowerShell


Leave Your Observation

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