An analogue of cd and chdir in PowerShell. How to change the current working folder in PowerShell
September 4, 2024
Why change the working folder in the command line
The current folder (or working folder) is the location in the file system where the user is located when working in the Terminal.
Strictly speaking, each running process has its own working folder (at startup, it coincides with the folder where the file or script that launched this process is located, but it can be changed).
And in fact, when changing the working directory (or current directory), the user does not physically move anywhere – only the value of the environment variable containing data about the current folder at the moment ($PWD) changes.
For details, see the article: How to find out the current directory in the Windows command line. Analog of pwd in PowerShell
From a practical point of view, the working folder is important when working with files (reading files or writing to files) when relative paths in the file system are specified for them. If you specify absolute paths to files, it doesn't really matter what folder you're in.
Set-Location – analogous to cd and chdir
To change the working folder in PowerShell, use the Set-Location cmdlet. As an option, specify the path in the file system where you want to go:
Set-Location C:\Users\MiAl\Downloads\
By default, the Set-Location cmdlet does not output anything, but the command prompt and the terminal window title may change (this depends on the programs used and user settings).
When using wildcards, the cmdlet selects a container (directory, registry key, certificate store) that matches the wildcard pattern. If the wildcard pattern matches more than one container, the cmdlet returns an error.
You can use the -Path option to specify the path, but this will not change the result in any way:
Set-Location -Path C:\Users\MiAl\Downloads\
The -Path option is not as useless as it seems at first glance. Another use for it will be discussed below.
When you run this cmdlet without options, it will go to the user's home folder (the same one that is the current directory when you open the terminal):
Return to the previous working folder
Using the -Path option and the “-” and “+” signs, you can navigate through the history of working folders. For example, to return to the previous working folder, run the command:
Set-Location -Path -
PowerShell keeps a history of the last 20 locations you set. If the value of the Path parameter is the “-” symbol, then the new working location will be the previous working location in the history (if it exists). Similarly, if the value is a “+” symbol, the new workspace will be the next workspace in the history (if it exists). This is similar to using Pop-Location and Push-Location, except that the history is a list rather than a stack, and is tracked implicitly rather than manually controlled. There is no way to view the history list.
Changing the folder and displaying information about the new working folder
If you add the -PassThru option to your command, then immediately after changing to the new folder, information about its location will be displayed:
Set-Location -PassThru C:\Users\MiAl\Downloads\
Related articles:
- How to find out the current directory in the Windows command line. Analog of pwd in PowerShell (100%)
- How to clear the terminal window in PowerShell. Analog of clear (65.8%)
- How to read file contents in PowerShell. How to save file contents to a variable (65.8%)
- How to display all environment variables at the Windows command prompt (52.2%)
- How to manage services on Windows (52.2%)
- How to install Apache web server with PHP, MySQL and phpMyAdmin on Windows [updated: September 2024] (RANDOM - 50%)