Even on Windows, you occasionally have to use the command line. Maybe you have to run a batch file, run a script, do some stuff with Git, Docker, or some other CLI tool. At the end of the day, you end up here:

And you wonder:
How do I switch to another directory on the command line?
But before you open up a command prompt, you should decide: Do you need elevated / administrator privileges?
Sometimes what you're trying to do will work even as a non-admin. If uncertain, try it as a regular user first. And if that doesn't work, restart Command Prompt and run it as an admin.
How to Run Command Prompt with Administrator Privileges
Just in case you need to run the cmd
application with administrator rights, this is what you have to do.
Press the Windows logo key (or simply click on the Start Menu). Then immediately start typing:
cmd
And you'll see something like this (depending on your version of Windows):

If you need to run Command Prompt as an admin, just click on Run as administator
. Then click Yes
when you're asked if you want to allow the app to make changes to your device.
If you don't need to run as admin, just click on Open
to run it as a regular user.
How to Change Directory in Command Prompt (cmd)
First, what directory / folder do you want to switch to? What is the full path to that directory?
One way you can find out: Where is your file located? The one that you are trying to run in Command Prompt.
You can navigate to it in File Explorer, and then right click on that file.

Next, click on Properties
. You can also do the same with a directory, instead of a file. Just navigate to the directory, right click on it, click on Properties
, and take the same steps below.
Here, look for Location
.

This shows you the full path to the directory. Select that location with your mouse, right click, and then click on Copy
.

Now go back to Command Prompt. And to switch to this example directory, C:\Users\Alex\.ssh
type this at your command line:
cd
Then add one space
character after it.

Next, right click your mouse inside this Command Prompt window (anywhere in the black area of this window). This should instantly paste the directory location you copied earlier. And you'll end up with this command (in this example):
cd C:\Users\Alex\.ssh

And that's it, press Enter, and you're there. You switched to another directory.

But notice that C:
at the beginning. Windows labels drives this way. C:
is usually your main drive, where Windows is installed. But you could have extra drives like D:
, E:
, and so on. Which might be separate partitions on your SSD. Or other drives entirely, like additional hard-disks you might have.
Either way, if you need to switch to a directory that is not located on C:
, you need to use a different command.
How to Change Drive Letter in Command Prompt (cmd)
You have two solutions here. One is to simply type the drive letter, followed by the :
colon character. For example, to switch to D:
you type:
D:

Then you press Enter, and just like that, you're now exploring the D:
drive.

Next, you can switch to the directory you want with the same cd
command explored earlier. For example, to change to a directory called FL Studio
, you can use this command:
cd D:\FL Studio

But wouldn't a simple cd
command work? Without switching the drive beforehand?
What if you are currently on the D:
drive and you want to switch to a directory on E:
Imagine you want to switch to the E:\OBS
directory, and you run this command:
cd E:\OBS
Well, this is what happens:

Note you're still in the old directory; still on the D:
drive.
A simple cd
command will only switch you to that directory if it's located in the same drive (letter) you currently have open in Command Prompt. But there's a simple solution to this.
You can make the cd
command change directories, AND the drive letter at the same time if you add a command line switch called /D
.
So instead of running cd E:\OBS
, you add a /D
in there before your full directory path, like this:
cd /D E:\OBS

See? The first variant, without /D
did not work. But the one with /D
did the job. You're now in E:\OBS
.
If you forget this /D
switch, just type this at the command line to get help options for the cd
command:
cd /?

Additional Commands
Here are some additional commands that might be useful when you change directories with cd
.
List Directory Contents
You switched to a directory, and you want to see what it contains. You can list directory contents with the dir
command:
dir

In this case, bin
, data
, and obs-plugins
are other subdirectories, because they're labeled as <DIR>
.
In case you're wondering what .
and ..
are, those are "fake directories" where .
points to the current directory — E:\OBS
in this case. And ..
points to the parent directory — E:\
in this example.
You will see how ..
can be used in the next section.
uninstall.exe
is a file because it is not labeled as <DIR>
, and you can also see its file size, 148,029
bytes.
Move One Directory "Up"
As you can see, directory paths can get quite long. Imagine you're in this directory:
E:\OBS\bin\64bit\platforms
And you want to move to the parent directory, or one directory "up":
E:\OBS\bin\64bit
One way would be to use a command like cd E:\OBS\bin\64bit
. But that's a lot to type, for such a simple task. Well, that's where that "fake directory" called ..
can come in handy.
Since ..
always points to the parent directory, no matter what directory you're in, you can use it in a cd
command to move one directory up.
So if you want to go from this to this:
E:\OBS\bin\64bit\platforms
E:\OBS\bin\64bit
You can tell cd
to move one directory up (move to the parent directory) with this command:
cd ..

Change Directory to Desktop in Command Prompt
Since many users like to keep stuff on their desktop, here's a useful shortcut.
Normally, when you open Command Prompt, you're already in your user's directory; something like C:\Users\Alex
. And the Desktop
directory is right below that, so you can switch to it without typing the full path.
Meaning, if you're here:

You can simply type:
cd Desktop
Instead of the much longer full path C:\Users\Alex\Desktop
. When you type cd Desktop
you use what is called a relative path.
And bam! You're in your desktop directory:

Then you can do the same to dive one directory below. If you have a directory called Projects
on your desktop, you can switch to it with a command like:
cd Projects
But what if you're in an entirely different location? A different directory (not user directory), and even a different drive?
Well, you can use the same trick mentioned earlier. The /D
switch to tell cd
to also change the drive letter. And you can use another trick, %USERPROFILE%
. This is a variable that points to your user's directory.
So to quickly switch to your desktop directory, you can run:
cd /D %USERPROFILE%\Desktop
How to Change Directory in PowerShell
The cd
command works in a similar way in both Command Prompt, and PowerShell. But there are a few key differences.
One thing cd
is sensitive about in PowerShell is that no space characters are allowed in a directory path. So if you want to switch to a directory like C:\Users\Alex\3D Objects
, this command won't work:
cd C:\Users\Alex\3D Objects

Simply because there is one space character in the 3D Objects
directory name. But this is easily fixable.
To switch to a directory path that contains space characters in PowerShell, just wrap that path between " "
double quotes.
Wrong command:
cd C:\Users\Alex\3D Objects
Correct command:
cd "C:\Users\Alex\3D Objects"
And now it works:

To switch to a directory located on another drive (letter), things are actually simpler in PowerShell. You don't need the /D
command line switch, like you needed in Command Prompt.
So even if you're on C:
, and you want to switch to a directory located on D:
, it's as easy as typing the full directory path after the cd
command.
Example:
cd E:\OBS

And you're there, easy as that!
Just remember, if that path has any space character, wrap it between " "
double quotes. If it doesn't have any space, double quotes are not needed.
Finally, to move to the parent directory (one directory "up"), the syntax is the same as Command Prompt:
cd ..

Conclusion
We hope this was useful. And just in case you need Command Prompt to do some stuff with Docker, check out this beginner-friendly course on Docker:

Or are you doing stuff with Git? We have that covered too!

Thank you for reading, and see you in the next blog!
Discussion