My PowerShell prompt’s currently pointed to my C drive (PS C:\>
). How do I change directory to a folder on my Q (PS Q:\>
) drive?
The folder name on my Q drive is «My Test Folder».
asked Dec 13, 2016 at 20:50
1
Unlike the CMD.EXE CHDIR
or CD
command, the PowerShell Set-Location
cmdlet will change drive and directory, both. Get-Help Set-Location -Full
will get you more detailed information on Set-Location
, but the basic usage would be
PS C:\> Set-Location -Path Q:\MyDir
PS Q:\MyDir>
By default in PowerShell, CD
and CHDIR
are alias for Set-Location
.
(Asad reminded me in the comments that if the path contains spaces, it must be enclosed in quotes.)
answered Dec 13, 2016 at 20:55
Jeff ZeitlinJeff Zeitlin
9,8582 gold badges21 silver badges33 bronze badges
3
To go directly to that folder, you can use the Set-Location
cmdlet or cd
alias:
Set-Location "Q:\My Test Folder"
answered Dec 13, 2016 at 20:54
BenHBenH
9,7761 gold badge22 silver badges36 bronze badges
0
Multiple posted answer here, but probably this can help who is newly using PowerShell
SO if any space is there in your directory path do not forgot to add double inverted commas «».
wonea
4,79317 gold badges86 silver badges139 bronze badges
answered Nov 14, 2017 at 13:16
DeepeshDeepesh
5906 silver badges8 bronze badges
2
You can simply type Q:
and that should solve your problem.
answered Dec 13, 2016 at 20:53
5
Set-Location -Path 'Q:\MyDir'
In PowerShell cd
= Set-Location
phuclv
38.2k15 gold badges157 silver badges477 bronze badges
answered Jan 9, 2018 at 9:57
Rao AdnanRao Adnan
1,60712 silver badges19 bronze badges
2
You can also use the sl
command to be able to change directories. It is Set-Location
but it is much shorter.
Example:
# Too verbose
Set-Location -Path C:\
# Just the right amount of characters to type
sl C:\
tdy
37.1k22 gold badges87 silver badges85 bronze badges
answered Oct 31, 2021 at 4:09
jaycedotbinjaycedotbin
3213 silver badges4 bronze badges
I don’t know why everyone talks about Set-Location
and the fact that cd
does not change drive and directory, in fact it actually does it (in powershell, not cmd), you just need to put quotes (single or double) around if there are spaces in folder name(s), also you can just type drive letter if you just want to go to its root:
Edit: now I started editing my PowerShell scripts with a «real» IDE I understood why everyone talks about Set-Location
, cd
is just an alias to it:
answered Apr 25 at 6:22
gluttonygluttony
4026 silver badges14 bronze badges
If your Folder inside a Drive contains spaces In Power Shell you can Simply Type the command then drive name and folder name within Single Quotes(»):
Set-Location -Path 'E:\FOLDER NAME'
The Screenshot is attached here
answered Feb 25, 2020 at 3:58
- On Powershell use Set-Location instead of cd.
- Put path in quotes. Single quotes works for me.
Set-Location ‘C:\Program Files\MongoDB\Server\6.0’
answered Sep 2, 2022 at 4:20
MonikaMonika
4314 silver badges6 bronze badges
In Windows, changing directories is a fundamental part of file management, but would you settle for writing full paths of each directory you visit? Luckily, PowerShell change directory commands can make your life easier.
In this tutorial, you will explore the commands available to change directories in PowerShell, helping you navigate your file system effortlessly.
Read on and master the basics of file system navigation with PowerShell today!
Announcing a Free LIVE training – Starting your PowerShell Journey – presented by Johan Arwidmark. Understand how PowerShell skills enhance your IT career, learn where to start with PowerShell, build your first scripts, and ask Johan questions directly in a live training environment.
Prerequisites
This tutorial will be a hands-on demonstration. To follow along, ensure you have a Windows system with PowerShell installed. This tutorial uses Windows 10 and PowerShell v7.
Switching from One Drive to Another
Each storage device, such as a hard disk or USB drive, is assigned a drive letter (i.e., C:, D:, E:, …Z:). When you open a PowerShell window, the prompt is in your user profile directory on the current drive by default.
Suppose you need frequent access to files or folders stored on a different drive than the one you are currently on. If so, switching from one drive to another is necessary.
To see how to switch from one drive to another:
1. Open PowerShell and confirm if the prompt says C:\Users\admin. If so, then you are currently on the C: drive in the admin user’s profile directory, as shown below.
2. Next, open another PowerShell session, but this time, as an administrator.
Below, the prompt starts in a different default directory than launching PowerShell as a regular user. Specifically, the prompt starts in the System32 directory within the Windows directory on the current drive (C:), as shown below.
This default behavior prevents non-administrative users from accidentally modifying critical system files or running sensitive tools.
3. Run the below command to change the current drive to D:
(or to any available drive). This syntax is a quick way to navigate between different drives in PowerShell.
When switching drives in PowerShell with this method, the prompt changes to the root directory of the new drive, as in the output below.
As you can see, regardless of the default directory that PowerShell starts in, you always can switch to another drive.
4. Now, run the below command to attempt switching to the D:\Kitty
directory (or to any other subdirectory).
Did you get the same error as below? This error shows since you cannot switch to subdirectories (for example, D:\Kitty) in one go by this method.
Jump to the following section to see how to switch to subdirectories.
Navigating the File System via the cd
Command
Navigating multiple subdirectories is a common task in PowerShell. But is there a quick way to navigate through different subdirectories? Yes! The cd
command, short for “change directory,” lets you quickly switch to another subdirectory regardless of your current directory.
Back in the day, the
cd
command was used to navigate between drives and directories in the Command Prompt. Microsoft made thecd
command backward compatible so you can still navigate between directories in PowerShell.
Read on and see how to navigate your file system with the cd
command.
Moving to a Directory in a Different Drive
Jumping to a subdirectory within a different drive may seem like a hassle if you think about it. Well, there is no need to overthink it. You will be surprised how quickly the cd
command lets you jump to another drive while moving into one of its directories.
Run the below command to navigate to the subdirectory called Kitty
in the D:*
drive.
Moving One to Many Levels Up in the Directory Hierarchy
With file system management, or when working on a project, you may frequently need to move up one or two levels from your current directory. The cd command supports the ..
notation that lets you effectively switch to the parent directory of your current directory.
This feature lets you move up a level in your file system without specifying the full path to the parent directory.
1. Run the cd
command below, append a space and the ..
notation, which tells PowerShell to go up one level in the directory hierarchy.
The space character after the
cd
command is not required, as the command will work fine without it. But adding a space is a common practice for better readability and consistency with other commands.
In the output below, you can see the prompt moves up one level from the C:\Users\admin\Documents to the C:\Users\admin directory. Mind you that you did not have to specify the full path for the C:\Users\admin directory to move up.
2. Instead of moving one level up, run the following command with two ..
notations, appending a backslash (\
) at the end of each. Doing so lets you move up two levels in the directory hierarchy.
Below, the output shows you jumped from the C:\Users\admin\Documents straight to the C:\Users directory.
3. Lastly, run the command below to move to the root directory of your current drive.
If you got lost in your file system, this command comes in handy as you can quickly jump to the root directory. This command saves your the hassle of running the cd ..
command multiple times.
Moving Into a Specific Subdirectory
Like moving a level up in the directory hierarchy, moving into a specific subdirectory is also a quick task. Since there might be many subdirectories, appending notations like the previous ones (..
, and ..\
) will not work, you must specify the subdirectory’s name.
Run the below command to move into one of the subdirectories (\admin
) in your current directory (.
).
Notice below that you saved time and effort since you did not have to specify the full path of the C:\Users\admin directory.
Navigating the File System with PowerShell Cmdlets
Although the cd
command undoubtedly works in PowerShell, Microsoft has introduced many cmdlets, like Set-Location
, that let you navigate through your file system.
These cmdlets provide more robust functionality than the cd
command that you can use to switch between drives, navigate stacked locations, and cycle through directories in the location history.
Stay tuned and witness how powerful PowerShell cmdlets are in navigating your file system.
Setting a Location from a Different Drive
Like the cd
command, the Set-Location
cmdlet allows you to jump from one directory to another and navigate straight to one of its directories. But since you are using a PowerShell cmdlet, you must append the -Path
parameter to set the path of your target location.
The Set-Location
cmdlet sets the current working directory to a specified location. That specified location can be one of the following:
- A directory, or subdirectory on the local file system.
- Any other location supported by a provider in PowerShell.
- Or a registry location.
To see how the Set-Location
cmdlet works in navigating your file system:
Run the below command to navigate to the Kitty
directory in your D:
drive specified in the -Path
parameter.
Set-Location -Path "D:\Kitty"
If you feel a bit lazy typing the full
Set-Location
cmdlet, you can also use one of its aliases, likechdir
andsl
, to navigate your file system. Aliases provide a more convenient way to access frequently used commands.
Now, run the below command to set the working location to a specific registry location. In this case, the HKLM:\
path (a shorthand) refers to the HKEY_LOCAL_MACHINE hive in your Windows Registry.
This command sets the working location to the root of the HKEY_LOCAL_MACHINE hive in the registry. Navigating to a registry allows you to access and modify its subkeys and values via other PowerShell commands.
Set-Location -Path "HKLM:\”
Switching Between Stacked Locations (Push and Pop)
Quickly accessing stacked locations sounds convenient. But is it possible? Yes! The Push-Location
and Pop-Location
cmdlets let you push and pop locations in a last-in-first-out (LIFO) order from a named stack.
To navigate stacked locations, follow these steps:
1. Run the below Push-Location
command to push your current location to a stack called Paths
and set the C:\Program Files\PowerShell\
directory as the current directory.
Push-Location -Path 'C:\Program Files\PowerShell\' -StackName "Paths"
2. Next, run the command below to push your current location (C:\Program Files\PowerShell\) to the top of the same Paths
stack.
Push-Location -Path 'C:\Program Files\WindowsPowerShell\' -StackName "Paths"
3. Run each command below to set Paths
as the default stack and view (Get-Location
) the stacked locations.
# Sets the Paths stack as default
Set-Location -StackName "Paths"
# View all stacked paths in the default stack
Get-Location -Stack
Below, you can see you currently have two locations stacked, where the last location you pushed is at the top.
4. Now, run the following Pop-Location
command twice to pop the top stacked location from the default stack, which currently is the Paths stack.
Note that popping the location from the stack removes the location from the stack. The error below indicates that you have popped all stacked locations from the default stack.
Navigating to a Directory Stored in a Variable
You have gone through different ways to navigate directories in your file system. But what if you need to navigate a specific location frequently? Typing full paths multiple times or cycling through the location history can be tedious. Why not store a directory’s full path in a variable instead?
With a declared variable, you can navigate to your target directory without specifying the path off the top of your head.
1. Run the below commands to store the C:\Program Files\PowerShell\
directory to the $psh
variable, and set that location ($psh
) as the current directory.
# Store a directory path to a variable
$psh = 'C:\Program Files\PowerShell\'
# Set the directory from the $psh variable as the current directory
Set-Location -Path $psh
The output below confirms successful directory change using a variable. Regardless of your current directory, you can quickly navigate to the C:\Program Files\PowerShell\ directory via the $psh
variable.
But note that this method only works for the current PowerShell session unless you persist the variable, as demonstrated in the following step.
2. Next, run the below commands, which do not provide output, but persist the ev_psh
variable as an environment variable (SetEnvironmentVariable
) with the value of the directory’s full path.
# Add psh as an environment variable
[System.Environment]::SetEnvironmentVariable('ev_psh', 'C:\Program Files\PowerShell\', 'Machine')
# Reload the newly-added environment variable (ev_psh)
$Env:ev_psh = [System.Environment]::GetEnvironmentVariable("ev_psh","Machine")
3. Lastly, run the below command to set the value from the ev_psh
environment variable as the current directory. Ensure that you are in a different directory than the one in your ev_psh
variable.
Set-Location -Path $env:ev_psh
Cycling Through Directories in PowerShell’s Location History
Another quick way to cycle through directories in PowerShell is using the location history. The -Path
parameter tells the Set-Location
cmdlet to navigate to another directory from the location history (previous and following), depending on the specified value (-
or +
).
To cycle through directories in PowerShell’s location history:
Run the following commands set locations from the history as the current directory. The +
character takes you forward in location history, while the -
character takes you back.
# Sets the system root directory as the current directory
Set-Location -Path $env:SystemRoot
# Navigates back to the previous directory in history (certificate provider)
Set-Location -Path -
# Navigates back to the previous directory in history again (system root directory)
Set-Location -Path -
# Navigates forward to the following working directory in history (certificate provider)
Set-Location -Path +
# Navigates forward to the following working directory in history (HKEY_LOCAL_MACHINE hive)
Set-Location -Path +
Announcing a Free LIVE training – Starting your PowerShell Journey – presented by Johan Arwidmark. Understand how PowerShell skills enhance your IT career, learn where to start with PowerShell, build your first scripts, and ask Johan questions directly in a live training environment.
Conclusion
File system navigation can be tricky when your options are limited. But with PowerShell change directory commands, like Set-Location
, you have plenty of options. How you wish to navigate your file system more efficiently is entirely up to you.
In addition, mastering the use of Push-Location
and Pop-Location
to save and recall recent locations is a valuable skill.
These techniques allow you to move quickly and seamlessly through directories and locations in PowerShell. But to further improve, why not consider learning about additional commands, like Get-ChildItem
, which can save you plenty of time in your daily tasks?
I read this answer but it doesn’t work for Windows PowerShell. How to change the drive?
EDIT:
Please ignore this question. I was trying to change the drive from C: to D:. The reason it wasn’t working was that there was no D: on my computer. A really silly mistake.
asked Jun 22, 2013 at 14:29
3
PowerShell will resolve full paths just fine with CD.
Where in DOS you have to /d for drive changing:
cd /d H:\Folder
PowerShell you can just type the full path:
cd H:\Folder
answered Jun 22, 2013 at 14:33
1
It’s the same as the DOS shell “cd dir”
answered Jun 22, 2013 at 14:32
3
Not the answer you’re looking for? Browse other questions tagged
.
Not the answer you’re looking for? Browse other questions tagged
.
This tutorial guides you how to change drive and the current directory in Windows PowerShell to different drive or directory.
There are many ways to change drive and directory in Windows PowerShell. The following are the different ways to do this.
Set-Location
Set-Location can be used to set current working location to a specified location as shown below.
Set-Location [[-Path] <String>] [-PassThru] [-UseTransaction] [<CommonParameters>]
For Example, Set-Location -Path E:/Work will set current working location to E:\Work
Windows PowerShell Copyright (C) 2014 Microsoft Corporation. All rights reserved. PS C:\Users\sneppets> Set-Location -Path E:/Work PS E:\Work>
To know the path of your current directory location, you can use the following command Get-Location
Windows PowerShell Copyright (C) 2014 Microsoft Corporation. All rights reserved. PS C:\> Get-Location Path ---- C:\
Note, built-in Windows PowerShell aliases like “cd“, “chdir” can be used instead of “Set-Location” which is explained above.
cd command
The following example shows how to use “cd” instead of “Set-Location” to change the current working directory.
PS C:\Users\sneppets> cd -Path E:/Work PS E:\Work>
chdir command
As said, “chdir” can also be used instead of “Set-Location” from PowerShell to change the directory as shown below
PS C:\Users\sneppets> chdir -Path E:/Work PS E:\Work>
Also See:
- Visual Studio Code change Terminal from powershell to cmd
- console.log() of an HTML element to know properties and events
- How to drop a mongodb database from mongo shell and command line?
- Import CSV using mongoimport command in MongoDB
- List all collections from mongo shell
References
- Managing current location
- Set-Location
Table of Contents
- Using Set-Location Cmdlet
- Use cd Alias
- Use chdir Alias
- Use sl Alias
- Using Push-Location Cmdlet
- Using Pop-Location Cmdlet
Using Set-Location
Cmdlet
Use the Set-Location
cmdlet to change directory in PowerShell.
Above, we have used the Set-Location
cmdlet to change the directory from C:\ to D:\ drive. In PowerShell, Set-Location
is used to specify the current working directory. First, the working directory was C:\Users\DELL after using Set-Location -Path D:\
current working directory changed to D:\. Here, the -Path
parameter was used to specify the path we wanted to navigate. Let’s see another example:
In this example, we used set-location
to change the current directory, which was D:\ in our case, to F:\ drive.
We have used
set-location
instead ofSet-Location
and-path
instead of-Path
. It means PowerShell is case-insensitive in terms of commands and parameters. It can be used in either lower-case or upper-case.
One thing is also important to note -path
is an optional parameter. We can also omit it. Consider the below example to see this.
In the above command, set-location
was used without the -path
parameter to change the current directory (F:\) to C:\ drive. Will this command work if we have a space in the directory path? Consider the following example where we want to change a directory to a path having space in it like D:\U – A. Check below to see how to do this:
In the above code, there is a space in the path; that’s why we enclosed it in double quotes. Likewise, in PowerShell, if there is a space in the directory path where you want to navigate, enclose the path in double quotes as "D:\U - A"
.
Set-Location has three aliases that can be used instead of
Set-Location
to change the current working directory. The three built-in PowerShell aliases ofset-location
are:sl
,cd
, andchdir
. Each of them can be used by replacing withSet-Location
.
Let’s use these aliases one by one and see how it works.
Use cd
Alias
Use the cd
alias to change a directory from C:\Users\DELL
to D:\Downloads\Sublime
in PowerShell.
cd —Path D:\Downloads\Sublime |
PS D:\Downloads\Sublime> |
Here, the cd
alias was used to change the directory. Our current working directory is the Sublime
folder inside the Downloads folder in the D:\ drive. Let’s consider a scenario where we want to drop one step down in the directory hierarchy. For this, use cd..
; have a look at the below example to see how to use it:
In the PowerShell snippet, cd..
was used to drop one level down from the current working directory. So first, we were in the D:\Downloads\Sublime directory; after using the cd..
command, the directory dropped one level down to D:\Downloads.
Now if we want to come directly to the root
directory from any folder inside the current directory using cd\
instead of cd..
as shown in the below example:
Here, cd\
was used to navigate from D:\Downloads\Sublime to the root
directory D:\.
We have seen cd
work the same way as the set-location
cmdlet, but there is one minor difference between them. If we want to drop one level down in our path hierarchy using the set-location
cmdlet, we need to add a space between the command and the two periods(..
), unlike cd..
; otherwise, an error will occur.
If we execute the above command, the console will display an error. So, the correct command is as follows:
Our directory was changed from D:\Downloads to D:\.
Use chdir
Alias
Use the chdir
alias to change a directory from C:\
to D:\Downloads\Sublime
in PowerShell.
chdir —Path D:\Downloads\Sublime |
PS D:\Downloads\Sublime> |
In this example, the chdir
alias navigated from C:\ drive to D:\Downloads\Sublime path. Now suppose we want to navigate to a subfolder from our current working directory, but we are not sure of the exact path of the folder; for that purpose, there is a Get-ChildItem cmdlet in PowerShell that can be used to see all the folders inside the current folder. As shown in the example below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
Directory: D:\Downloads\Sublime Mode LastWriteTime Length Name —— ——————— ——— —— d——— 7/7/2020 1:13 PM Packages —a—— 9/1/2020 2:32 PM 59981 331ffde2—9273—4446—82f0—04aee4abce7e.dmp —a—— 9/1/2020 2:28 PM 58777 59dd56f7—2f0b—42d2—b1d4—80c59dc286f3.dmp —a—— 10/1/2019 9:20 PM 56720 changelog.txt —a—— 10/1/2019 10:35 PM 232336 crash_reporter.exe —a—— 10/12/2018 6:11 AM 773968 msvcr100.dll —a—— 10/1/2019 10:35 PM 6644112 plugin_host.exe —a—— 10/12/2018 6:11 AM 2628080 python3.3.zip —a—— 10/12/2018 6:11 AM 5420032 python33.dll —a—— 10/1/2019 10:35 PM 451472 subl.exe —a—— 9/17/2019 7:44 PM 38852 sublime.py —a—— 9/17/2019 7:44 PM 36887 sublime_plugin.py —a—— 10/1/2019 10:35 PM 6896528 sublime_text.exe —a—— 7/7/2020 1:13 PM 10921 unins000.dat —a—— 7/7/2020 1:13 PM 1189776 unins000.exe —a—— 7/7/2020 1:13 PM 22709 unins000.msg —a—— 10/1/2019 10:35 PM 160656 update_installer.exe |
As shown in the above example, the Get-ChildItem
was used to display the list of all the folders inside the D:\Downloads\Sublime directory. So now, we can navigate to any folder where we want to move.
DIR
is an alias ofGet-ChildItem
. We can use it to see the list of folders within the current working directory.
Use sl
Alias
Use the sl
alias to change a directory from D:\Users\DELL
to C:\\
in PowerShell.
Using Push-Location
Cmdlet
To change a director in PowerShell:
- Use the
Get-Location
cmdlet to get a current working directory. - Use
Push-Location
to push the current working directory to stack and make the given directory the current working directory. - Use
Get-Location
again to see the current working directory.
Get—Location Push—Location —Path C:\ Get—Location |
How are we changing the directory in the above code? Before going into details, it is essential to know why the Push-Location
cmdlet was used. When we change the directory location, we often want to trace back to the recent location. In PowerShell, the Push-Location
cmdlet is used to keep track of a history of paths of directories where we moved in the form of a stack.
In the above code, we used Get-Location
to know the current working directory, which was D:\U – A\testing. Then, we used the Push-Location
cmdlet to push it to the stack and navigate to another folder/path, which we specified using the -Path
parameter.
After the execution of the Push-Location
cmdlet, the location path D:\U – A\testing was pushed into the stack, and the directory path was changed to C:\ drive. Then, to ensure the updated working directory, we used Get-Location
again and observed that the current location was successfully changed to the C:\ directory; see the above output.
Let’s say we want to move to the testing folder in D: \ drive.
Push—Location —Path «D:\U — A\testing» —PassThru |
In the above example, first, we were in C:\ drive. After using the Push-Location -Path "D:\U- A\testing"
command, the C:\ drive on which we were working moved to the stack, and the current working directory is changed to D:\U- A\testing. The -PassThru
parameter displayed the new path where we moved.
The PowerShell -PassThru
parameter was used with the commands with no default output. For the execution of set-location
or push-location
, the outcome is not displayed on the console. However, the directory changed. Check the next section to see how to return to our previous location.
Using Pop-Location
Cmdlet
To change a director in PowerShell:
- Use the
Get-Location
cmdlet to retrieve the current directory. - Use the
Get-Location
cmdlet with-Stack
to display all the directory paths we worked on. - Use the
Pop-Location
cmdlet to pull out the path inserted in the stack.
Get—Location Get—Location —Stack Pop—Location —PassThru |
Path —— D:\U — A\testing C:\ D:\U — A |
First, we used Get-Location
to know the current working directory; then, we used this cmdlet with the -Stack
parameter to show all the directories we have worked on. Finally, we used the Pop-Location
cmdlet to pull out the path inserted in the stack.
The above output demonstrated that our current working directory was initially D:\U- A\testing. The C:\ and D:\U – A were the location paths we previously worked on; note that these are the same paths we pushed in the stack in the above section.
Next, to go back to the previous directory on which we were working, which is stored in the stack, we used the Pop-Location
cmdlet and got the path out of the stack. We can observe that the C:\ drive was inserted last in the stack and popped out first. So we can verify the LIFO
concept here. So now our current working directory is changed to C:\.
Let’s pop out the remaining path from the stack.
The remaining directory path D:\U – A popped out from the stack and became our current working directory.