How to Change Directory – From the Command Prompt (CMD, Batch Script, Drive Letter)

What We'll Learn:

Welcome!

This short guide is all about learning how to change your current directory from the windows command prompt (cmd).

  • We are going to learn, the three ways you can change directory from the command prompt (cmd).
  • How to change your drive directory letter.
  • And how to change the directory of a batch script.

Lets get started!

Change Cmd directory #1: The Easy Way (Drag & Drop)

By far the easiest way to change the current directory on your windows command prompt is by using the drag and drop method.

Here is how you do that:

  • 1) First find the directory you want to navigate into from the windows explorer.
  • 2) Now open your command line and type CD followed by a single space:
    Cd

    Cd simply stands for “Change Directory”.

  • 3) Finally, drag and drop the directory you want to navigate into to your command prompt.

    The location or path of the directory you want to navigate into will appear automatically.

    All you have to do then is press ender.


Note that the drag and drop method only works in a windows command prompt (cmd) and not a PowerShell console.

And that’s it! You have now navigated into your target directory!

However, using the drag and drop method is not always practical, so let’s see how we can change our directory from the command prompt itself.

Change Cmd directory #2: The Simple Way (Dir & CD)

The drag and drop method is great, but in many cases you might want to change directory directly from the command prompt itself.

For this example, let’s assume that the file we want to navigate into is located within our desktop and is named “My Folder”.


Step 1:
Locate the directory you want to navigate into.

Simply use the Dir command to figure out to which directory you should navigate into so that you are one step closer to your target directory.

Dir

The Dir command simply displays every file and folder in the current directory.

By default, a non-elevated command prompt opens in the following location: C:\Users\Username

In our case we know that the file we want to navigate into is located within our desktop, so that is where we should navigate to next.


Step 2:
Use Cd to navigate to that directory.

To navigate there simply type cd followed by the directory of your choice within quotes.

Cd "Desktop"

Tip: Use the TAB key to autocomplete directory names.

Simple type the first few letters of the directory you want to navigate into and Press the TAB key to autocomplete the rest.

In case you want to navigate one directory backwards type cd followed by two dots

Cd ..


Step 3:
Repeat Steps 1 & 2 to navigate to your target directory.

In our case since we navigated into our desktop directory and since that is where our target directory is located we can simply navigate there by using the Cd command.

Cd "My Folder"

And that’s it! You might have to go backwards and forwards a few times until you figure out exactly where you need to navigate into each time, but if you have a rough idea of where your target directory is located at, you should be able to figure everything out.

However, it’s inefficient and time consuming, thankfully, there is a better way.

Change Cmd directory #3: The Best Way (Relative & Absolute Paths)

We can use relative and absolute paths to navigate exactly where we want to with just one command.

This assumes however that you know exactly where your target directory is located, if you do not, then you would have to use the drag and drop method or the Dir & Cd commands to figure it out, just like we learned before. Otherwise, lets go ahead!


Relative Paths:

In our previous example we used the Cd and Dir commands to navigate into a folder within our desktop directory. Let’s see how we can use a relative path to navigate to a directory with just one command.

Here is what that would look like:

Cd "Desktop/My Folder"

Like we said before each backslash on a path specifies the name of a directory.

What makes this path relative?

The fact that it only works when we are located within the same directory our desktop folder is located, in this case:

C:\Users\Username

Running this command from a different directory would return an error because the desktop directory couldn’t have been found. That’s where absolute paths come in.


Absolute paths:

We can navigate to our “My Folder” directory from anywhere by using an absolute path.

Absolute or full paths simply include every directory that comes before our target directory within them.

Here is what that would look like:

Cd "C:\Users\Username\Desktop\My Folder"

Pretty simple, isn’t it?

By using an absolute path, we can navigate to any directory regardless of where we are currently located.

Play Video

There's an easier way!

Learn the ins and outs of the windows command line with our windows command line course.

Change Drive Directory Letter (CMD).

By default the windows command prompt (cmd) operates on your C: drive, which is likely also where windows itself is installed.

But what if you want to navigate to a directory that is located within another drive?

Simply enter the letter of your drive followed by the colon : punctuation mark.

D:

Replace D with the letter of your drive.

From here, you can navigate to any directory within this drive, just like we learned above.

Change Script Directory (within a batch script file).

Apart from changing the directory of your windows command prompt (cmd) you can change the directory in which your batch script is being executed at.

Just like with the command prompt, batch scripts are executed in one of two locations:

  • A batch script without administration privileges gets executed at: C:\Users\Username
  • While a batch script with administration privileges gets executed at: C:\Windows\System32


So, for example if we want to change the location of a script without administration privileges into our desktop directory all we have to do is simply use the Cd command, just like we did on the command prompt itself.

Just add this line somewhere at the beginning of your script:

Cd "Desktop"

To change the directory of a script that has been opened with administration privileges you need to use an absolute path instead:

Cd "C:\Users\Username\Desktop"

Once again, replace Username with your computer’s username.

 

And that’s it! Your script will now be executed in the directory of your choice and you can use relative paths within it to reference other files and directories.

Simple Examples You Can Copy & Paste.

Navigate to the Program Files directory:

Cd "C:\Program Files"


Navigate to the desktop directory (Replace Username):

Cd "C:\Users\Username\Desktop"


Navigate to a folder named My Folder with the desktop directory (Replace Username):

Cd "C:\Users\Username\Desktop\My Folder"


Change drive directory letter to D (Replace D with the drive letter of your choice):

D:

Summary:

  • Drag & drop a folder into the windows command prompt (cmd) to autofill its path.
  • Use the Dir & Cd commands to sequentially navigate through directories until you end up in your target directory.
  • If you know exactly where your target directory is, use relative or absolute paths to directly change your directory.

That's It!

You now know how to change the current directory of your windows command line.

If you liked this short guide take a look at a few of our other posts related to the windows command line, or if you really liked it consider enrolling in our video course where you will learn the ins and outs of the Windows command Line.

Play Video
Dont Stop Learning!

Windows Command Line Course!

This course has everything you need to start learning about the windows command line along with batch scripting.

Read More:

Any Questions? Ask them Below:

Leave a Reply

Your email address will not be published.

Take Your Skills to the next level!
Become one of our beloved students by enrolling into one of our courses. Get started now!

WAIT UP!

Take A Look At Our Command Line Course!

Need Help?

There's an easier way!

Welcome

You are now officially on your way to becoming a genius!