pushd, popd

Maintained on

When you move to another folder with cd, you have to remember where you were if you want to go back.

pushd remembers the current folder and then moves, and popd takes you back to the remembered folder. They are handy when you want to work in another folder for a moment and then return to where you started.

Basic syntax

Syntax
pushd folder_path
popd
  • pushd: Remembers the current folder and moves to the specified folder.
  • popd: Returns to the most recently remembered folder.

Basic usage

pushd C:\Windows\System32
popd
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>cd
C:\Users\sample
C:\users\user>
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>pushd C:\Windows\System32
おすすめ書籍※ 広告を含む場合があります
中小企業経営者のためのRPA入門 RPA導入を成功させる方法

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超入門

60分でわかる! AIエージェント 超入門

コマンドラインの黒い画面が怖いんです。

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>cd
C:\Windows\System32
C:\users\user>
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>popd
- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>cd
C:\Users\sample
C:\users\user>

Running popd takes you back to C:\Users\sample, where you were before pushd.

Difference from cd

cd needs /d to move to a different drive, but pushd moves there directly. Another big difference is that pushd remembers the folder you left.

Remembered folders are stacked

Folders remembered by pushd are saved in a stack. The most recent one sits on top, and popd takes them off from the top.

Move
pushd C:\Windows
pushd C:\Users
pushd D:\work

Each popd from here walks back through the remembered folders, newest first.

  1. First popd: returns to C:\Users
  2. Second popd: returns to C:\Windows
  3. Third popd: returns to the folder you started in

View the stack

Running pushd with no arguments lists the remembered folders. This works in a normal environment where command extensions are enabled.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>pushd
C:\Users
C:\Windows
C:\Users\sample
C:\users\user>

The higher the line, the closer it is to the place the next popd returns to (the more recently remembered).

Move to a network path (UNC)

cd cannot move to a network path such as \\server\share. pushd can, because it automatically assigns a free drive letter.

Work
pushd "\\server\share\work"
dir
popd

The assigned drive letter is the first free one, starting from Z: and working backward. When popd returns you to the original folder, the assigned drive is released.

Running a batch file from a shared folder

If you run a batch file directly from a UNC path, the current directory cannot be set to the UNC path, and a warning may appear. Put pushd "%~dp0" at the top of the batch file to move to the folder that contains it before the main work begins.

Use it in batch files

When a batch file works in another folder, wrap that part in pushd and popd so the current directory is the same before and after.

backup.cmd
@echo off

pushd "%~dp0"
echo Folder of this batch file: %cd%

pushd "C:\data"
if not exist "%~dp0backup" mkdir "%~dp0backup"
copy *.txt "%~dp0backup\"
popd

echo Back in the batch file's folder: %cd%
popd

%~dp0 is the folder that contains the batch file itself. See

for details.

Don't forget popd

If the number of pushd and popd calls does not match, you may stay in a different folder after the batch file ends. Always use pushd and popd in pairs.

When the folder does not exist

If you give pushd a folder that does not exist, an error message appears and no move happens.

- □ ×
Command Prompt Icon
Command Prompt
Microsoft Windows [Version xx.x.xxxxx.xxx]
(c) 2026 Ribbit App Development All rights reserved.
 
C:\users\user>pushd C:\nothing
The system cannot find the path specified.
C:\users\user>

If you run popd after a failed move, it takes you back to a folder remembered earlier. In a batch file, it is safer to branch on failure with ||.

Stop
pushd "C:\data" || exit /b 1
rem Put your commands here
popd

See

for how || works.

In PowerShell

In PowerShell, pushd and popd are aliases for Push-Location and Pop-Location. The stack is managed separately from Command Prompt’s.

Summary

GoalCommand
Remember the folder and movepushd folder
Return to the remembered folderpopd
List remembered folderspushd (no arguments)
Move to a network pathpushd "\\server\share"
Move to the batch file’s folderpushd "%~dp0"

Quiz

Practice Problem

What happens when you run popd after pushd \\server\share?

Choose one answer.

  1. You return to the folder you were in before pushd, and the assigned drive letter is released
  2. The network connection is dropped and you stay where you are
  3. You move up one folder
#Command Prompt #Batch File #pushd #popd #Directory #Command