How Can I Return to the Previous Directory in Windows Command Prompt?

I often want to return to the previous directory I was just in in cmd.exe, but windows does not have the "cd -" functionality of Unix. Also typing cd ../../.. is a lot of typing.

Is there a faster way to go up several directory levels?

And ideally return back afterwards?

2

5 Answers

On Windows CMD, I got used to using pushd and popd. Before changing directory I use pushd . to put the current directory on the stack, and then I use cd to move elsewhere. You can run pushd as often as you like, each time the specified directory goes on the stack. You can then CD to whatever directory, or directories , that you want. It does not matter how many times you run CD. When ready to return , I use popd to return to whatever directory is on top of the stack. This is suitable for simple use cases and is handy, as long as you remember to push a directory on the stack before using CD.

1

Steps:

  1. pushd . (Keep old folder path on the stack)
  2. cd ..\.. (Move to the folder whare you like to)
  3. popd (Pop it from the stack. Meaning, Come back to the old folder)
1

Run cmd.exe using the /k switch and a starting batch file that invokes doskey to use an enhanced versions of the cd command.

Here is a simple batch file to change directories to the first parameter (%1) passed in, and to remember the initial directory by calling pushd %1.

md_autoruns.cmd:

@echo off
cd %1
pushd %1
title aliases active
cls
%SystemRoot%\System32\doskey.exe /macrofile=c:\tools\aliases

We will also need a small helper batch file to remember the directory changes and to ignore changes to the same directory:

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.