pushd, popd
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
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

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

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

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

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
Running popd takes you back to C:\Users\sample, where you were before pushd.
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.
pushd C:\Windows
pushd C:\Users
pushd D:\work
Each popd from here walks back through the remembered folders, newest first.
- First
popd: returns toC:\Users - Second
popd: returns toC:\Windows - 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.
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.
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.
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.
@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
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.
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 ||.
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
| Goal | Command |
|---|---|
| Remember the folder and move | pushd folder |
| Return to the remembered folder | popd |
| List remembered folders | pushd (no arguments) |
| Move to a network path | pushd "\\server\share" |
| Move to the batch file’s folder | pushd "%~dp0" |