pushd and popd are two CMD built-in commands that let you save your current directory to a stack, navigate elsewhere, and return to the saved path with a single command. They are especially useful in batch scripts where you need to change directories temporarily and return cleanly afterward.
Quick Reference
| Command | What it does |
|---|---|
pushd <path> | Save current directory to stack, change to <path> |
popd | Return to the last saved directory from stack |
pushd \\Server\Share | Map network share to a drive letter, change into it |
popd (after network pushd) | Disconnect the temporary drive mapping |
pushd (no arguments) | Display the current directory stack |
How pushd and popd Work
pushd pushes your current directory onto an internal stack and changes to the specified path. popd pops the last entry off the stack and returns you to it.
You can stack multiple pushd calls. Each popd unwinds one level:
pushd C:\Logs
pushd C:\Builds\ProjectA
pushd D:\Archive
popd
popd
popd
After the three popd calls, you are back at the directory you were in before the first pushd.
Running pushd without arguments displays the current stack contents.
Basic Usage
Starting directory: C:\Users\Administrator
pushd D:\Projects\AppX\src
You are now in D:\Projects\AppX\src. Your previous path is saved.
popd
You are back in C:\Users\Administrator. No need to remember or retype the original path.
CMD vs PowerShell
pushd and popd exist in both CMD and PowerShell, but behave differently.
CMD
- Built into
cmd.exeas internal commands - Stack is session-local and lost when the session ends
- Network path (
\\server\share) is automatically mapped to a temporary drive letter
PowerShell
Push-LocationandPop-Locationare the native cmdletspushdandpopdare built-in aliases for them- Support multiple location stacks via the
-StackNameparameter - No automatic drive letter mapping for UNC paths
Push-Location D:\Projects\AppX\src
# do work
Pop-Location
If you are writing scripts, use Push-Location / Pop-Location explicitly in PowerShell to make intent clear.
Network Share Mapping (CMD Only)
In CMD, when you pushd to a UNC path, Windows automatically maps it to an available drive letter:
pushd \\FileServer01\Deployments
Output:
Z:
Windows assigns the next available drive letter (e.g. Z:), changes into it, and saves the UNC path on the stack.
When you run popd, the temporary drive mapping is removed automatically.
This is useful in scripts that need to access file shares without managing drive mappings manually.
Using pushd and popd in Batch Scripts
This is where pushd and popd have the most practical value.
Pattern: temporary directory change
@echo off
pushd D:\Builds\ProjectA
call build.bat
popd
pushd D:\Builds\ProjectB
call build.bat
popd
Each block runs in its own directory context and returns cleanly. If build.bat changes the directory internally, popd still returns you to the saved path.
Pattern: script self-location
A common pattern in admin scripts: change to the directory where the script itself is located, regardless of where it was called from.
@echo off
pushd %~dp0
:: %~dp0 = drive and path of the script file
:: all relative paths now work correctly
call .\config\setup.bat
popd
This makes scripts portable and avoids broken relative paths when run from a different working directory.
Pattern: network deployment
@echo off
pushd \\DeployServer\Releases\v2.1
xcopy /E /I . C:\App\
popd
No manual net use required. pushd handles the mapping and popd cleans it up.
Real-World Use Cases
Patch and update scripts Scripts that apply updates to multiple directories use pushd/popd to move between targets and return to the base path after each operation.
Multi-step build automation Build pipelines written in CMD use stacked pushd calls to enter subdirectories for compilation steps, then unwind the stack when each step completes.
Admin maintenance tasks Interactive sessions where you temporarily inspect a remote share or a system path and want to return to your working directory without typing the full path again.
Tips and Pitfalls
pushd does not survive session end The stack exists only for the duration of the CMD or script session. It is not persistent.
Drive letter availability (network paths) If all drive letters are in use, pushd \\server\share will fail. This is rare but possible in environments with many mapped drives.
popd on an empty stack Running popd when the stack is empty does nothing in CMD. In PowerShell, it also does nothing but may produce a warning depending on the version.
Cross-drive navigation Unlike cd, pushd handles cross-drive path changes correctly without requiring the /d flag. pushd D:\Folder works from any drive.
Avoid mixing cd and pushd in scripts Using cd inside a script that also uses pushd/popd can lead to unexpected directory state. Stick to one navigation method per script.
