skip to content

cd — Change Directory

Navigate the Windows command prompt's current working directory. Covers drive switching, absolute and relative paths, UNC paths, and path-with-spaces quoting.

11 min read 63 snippets deep dive

cd — Change Directory#

What it is#

cd (also chdir) is a built-in cmd.exe command that changes the shell’s current working directory. It has been present since MS-DOS 1.0 and is available in every Windows version. Because cmd.exe tracks a separate current directory per drive, cd alone does not switch drives — you must also type the drive letter (e.g. D:) to make the new drive active.

Availability#

cd is built into cmd.exe on every Windows version. PowerShell has its own Set-Location (aliased cd and sl) which behaves slightly differently with drives and UNC paths.

cd /?

Output:

Displays the name of or changes the current directory.

CHDIR [/D] [drive:][path]
CHDIR [..]
CD [/D] [drive:][path]
CD [..]

Syntax#

cd takes an optional /D flag and a path argument. Without any argument it prints the current directory. .. steps up one level; multiple .. can be chained with backslashes.

cd [/D] [drive:][path]
cd ..

Output: (none when changing; prints current path when called with no argument)

Essential options#

FormMeaning
cd (no args)Print current working directory
cd pathChange to path on the same drive
cd /D pathChange directory AND switch the active drive
cd ..Go up one directory level
cd ..\..Go up two levels
cd \Go to the root of the current drive
cd -Not supported in cmd (use PowerShell cd -)

Printing the current directory#

Calling cd with no arguments outputs the current working path — useful inside batch scripts to log the working context.

cd

Output:

C:\Users\alicedev\Documents

Absolute and relative paths#

An absolute path starts from the drive root (\ or C:\). A relative path is resolved from the current directory — . is the current directory and .. is the parent.

rem Absolute path
cd C:\Windows\System32

Output: (none — exits 0 on success)

rem Relative: move into a subdirectory
cd Projects\myapp

Output: (none — exits 0 on success)

rem Go up one level
cd ..

Output: (none — exits 0 on success)

rem Go up two levels
cd ..\..

Output: (none — exits 0 on success)

rem Jump to drive root
cd \

Output: (none — exits 0 on success)

Switching drives#

In cmd.exe, cd changes the directory on a drive but does NOT make that drive the active one. Type the drive letter followed by a colon to switch the active drive, then cd into a folder on it.

rem Switch active drive to D:
D:

rem Then navigate
cd \Projects\myapp

Output: (none — exits 0 on success)

rem /D flag does both in one step: switch drive AND directory
cd /D D:\Projects\myapp

Output: (none — exits 0 on success)

Paths with spaces#

Paths containing spaces must be enclosed in double quotes.

cd "C:\Users\alicedev\My Documents"

Output: (none — exits 0 on success)

rem Works the same with relative paths
cd "My Documents\Q1 Reports"

Output: (none — exits 0 on success)

Environment variable expansion#

cd expands environment variables inside the path when % delimiters are used.

rem Jump to the user's home directory
cd %USERPROFILE%

Output: (none — exits 0 on success)

rem Jump to the Windows temp folder
cd %TEMP%

Output: (none — exits 0 on success)

rem Use a custom variable in a batch script
set PROJECT=C:\Projects\myapp
cd %PROJECT%

Output: (none — exits 0 on success)

UNC paths#

cmd.exe does not support UNC paths (\\server\share) as the working directory without /D and a mapped drive or a special workaround. The recommended approach is to map a drive letter first.

rem Map a network share to Z:
net use Z: \\myhost\share

rem Then cd into it
cd /D Z:\data

Output: (none — exits 0 on success)

Common pitfalls#

  1. cd D:\path without /D doesn’t change the active drive — the directory is remembered on D: but the prompt still shows C:. Add /D or type D: separately.
  2. Spaces in path without quotes silently failscd C:\My Documents is parsed as cd C:\My with trailing junk; always quote paths with spaces.
  3. cd does not support wildcardscd Project* produces an error; tab-complete or type the full name.
  4. UNC paths as working directory — most programs don’t support UNC as the CWD; map a drive letter with net use first.
  5. cd - is not a thing in cmd — use pushd / popd if you need a directory stack in batch scripts.

Real-world recipes#

set ROOT=C:\Projects\myapp
cd /D %ROOT%\src

Output: (none — exits 0 on success)

Save and restore directory in a batch script with pushd/popd#

pushd C:\Temp\build
rem ... run build commands ...
popd
cd

Output:

C:\Users\alicedev\Documents

Open a new cmd window in a specific folder from Explorer#

Right-click a folder → “Open in Terminal” (Windows 11) or address bar → type cmd → Enter. Alternatively from an existing prompt:

start cmd /K "cd /D C:\Projects\myapp"

Output: (new cmd window opens at the target path)

Per-drive current directory#

cmd.exe keeps a separate current directory for each drive letter, a legacy from MS-DOS. Typing D: after a previous cd D:\Work lands back in D:\Work rather than D:\. This is occasionally useful but more often surprising — most scripts should use cd /D to set both drive and directory in a single atomic step. The per-drive directories are stored as environment-style variables named =C:, =D:, etc., visible via set "=" from a process started with cmd /V:ON.

rem Start on C: and remember a working dir on D:
C:
cd C:\Users\alicedev
D:
cd D:\Work
C:
echo Now on C:, prompt = %CD%
D:
echo Now on D:, prompt = %CD%

Output:

Now on C:, prompt = C:\Users\alicedev
Now on D:, prompt = D:\Work
rem Inspect the hidden per-drive variables
cmd /V:ON /C "set ^"=^""

Output:

=C:=C:\Users\alicedev
=D:=D:\Work
=ExitCode=00000000

pushd, popd, and the directory stack#

pushd saves the current directory onto a per-shell stack and changes to a new directory in one step. popd returns to the most recently pushed location. Together they replicate the bash cd - idiom and stack-based navigation. Crucially, pushd is also the only built-in way to make a UNC path the working directory of cmd.exe: it transparently maps a temporary drive letter (starting at Z: and counting down) and switches to it.

rem Save current dir, jump elsewhere, come back
cd
pushd C:\Temp
echo Working in %CD%
popd
cd

Output:

C:\Users\alicedev\Documents
Working in C:\Temp
C:\Users\alicedev\Documents
rem Stack-based: push twice, pop twice
pushd C:\Logs
pushd C:\Temp
echo %CD%
popd
echo %CD%
popd
echo %CD%

Output:

C:\Temp
C:\Logs
C:\Users\alicedev\Documents
rem UNC paths: pushd silently maps a drive letter
pushd \\myhost\share\data
echo Drive after pushd: %CD%
popd

Output:

Drive after pushd: Z:\data

CD with the /D flag#

/D is the single most important cd flag in modern scripting. Without it, cd D:\Work from a C:\ prompt only updates the remembered current directory for D: — it does NOT switch the active drive, and %CD% still reports a path on C:. With /D, both drive and directory change atomically.

rem Without /D — surprising behaviour
C:
cd D:\Projects
echo %CD%

Output:

C:\Users\alicedev
rem With /D — does what you expect
C:
cd /D D:\Projects
echo %CD%

Output:

D:\Projects

Tab completion and command extensions#

Tab completion of directory names is enabled by default when Command Extensions are on. Pressing Tab cycles through matching entries; Shift+Tab cycles backwards. The CompletionChar and PathCompletionChar registry values under HKCU\Software\Microsoft\Command Processor can change the completion key globally.

rem At the prompt, type the partial name and press Tab
cd C:\Pro<Tab>

Output:

C:\Program Files\
rem Cycle through matches with repeated Tab presses
cd C:\Pro<Tab><Tab>

Output:

C:\Program Files (x86)\

CHDIR and case-sensitivity#

chdir is the original DOS form; cd is the modern shorter alias and both are identical. Windows filesystems (NTFS, FAT32, ReFS) preserve case but match case-insensitively by default, so cd documents and cd DOCUMENTS both work as long as either form resolves to an existing folder. NTFS volumes can be marked case-sensitive at the directory level via fsutil.exe file setCaseSensitiveInfo — a setting WSL relies on.

chdir C:\Windows
chdir

Output:

C:\Windows
rem Case-insensitive lookup
cd c:\users\ALICEDEV\documents
echo %CD%

Output:

C:\Users\alicedev\Documents

The CD environment variable trick#

cd writes the current directory into the dynamic environment variable %CD%, which can be used in scripts to capture the working directory before changing it. %CD% is read-only — assigning to it does not change directory.

set ORIG=%CD%
cd C:\Temp
rem ... do work ...
cd /D %ORIG%
echo Restored to %CD%

Output:

Restored to C:\Users\alicedev\Documents

PowerShell equivalents#

PowerShell uses Set-Location (aliases cd, chdir, sl) to change the current location. Unlike cmd.exe, PowerShell tracks a single current location across all drives — switching drives is just another Set-Location. PowerShell drives also include non-filesystem providers: HKLM:, HKCU:, Cert:, Env:, Variable:, Function:, Alias:. Set-Location accepts -Path, -LiteralPath, -StackName, and the special - argument (equivalent to bash cd -).

# Basic location change
Set-Location C:\Projects\myapp

Output: (none — silent success)

# cd is a PowerShell alias for Set-Location
cd C:\Logs

Output: (none — silent success)

# Go back to previous location with -
cd C:\Temp
cd -

Output:

Path
----
C:\Logs
# Push/pop a stack
Push-Location C:\Logs
Push-Location C:\Temp
Pop-Location  # back to C:\Logs
Pop-Location  # back to original

Output: (none — silent success)

# Navigate into a non-filesystem provider
Set-Location HKLM:\Software\Microsoft\Windows
Get-ChildItem

Output:

    Hive: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows

Name                           Property
----                           --------
CurrentVersion                 ...
# Change into a UNC path natively
Set-Location \\myhost\share\data
Get-Location

Output:

Path
----
Microsoft.PowerShell.Core\FileSystem::\\myhost\share\data

CMD vs PowerShell vs bash comparison#

GoalCMDPowerShellbash (Linux/macOS)
Print current dircd (no args) or echo %CD%Get-Location or pwdpwd
Change dircd pathSet-Location path or cd pathcd path
Change dir AND drivecd /D D:\pathcd D:\path (single namespace)(no drives)
Go to homecd %USERPROFILE%cd ~ or cd $HOMEcd ~ or cd
Go to previous dir(not built in)cd -cd -
Save / restorepushd / popdPush-Location / Pop-Locationpushd / popd
Navigate UNCpushd \\host\shareSet-Location \\host\share(mount first)
Parent dircd ..cd ..cd ..
Root of drivecd \cd \ (current drive)cd /
Tab completionYes (extensions on)Yes (and case-insensitive)Yes (typically case-sensitive)
Case-sensitive?InsensitiveInsensitiveSensitive

Common pitfalls (continued)#

  1. Per-drive current directories surprise scriptscd D:\work from C:\ does not switch drives. Always use cd /D in scripts that may run across drives.
  2. pushd to a UNC consumes drive letters — repeatedly pushing UNC paths without popd exhausts available drive letters from Z: down. Always pair pushd with popd.
  3. Long paths over 260 charscd may report success but subsequent commands fail. Enable LongPathsEnabled or use the \\?\ prefix from PowerShell.
  4. cd does not follow CDPATH — unlike bash there is no CDPATH lookup; only the current directory and absolute paths are considered.
  5. Working directory of a Scheduled Taskcd in the task action does not survive after the action completes; set the “Start in” field in the task properties instead, or use pushd inside a wrapper script.

Real-world recipes (continued)#

Set up a project alias with doskey#

doskey macros are per-session shortcuts. Combine with cmd /K to create persistent project navigators.

doskey myapp=cd /D C:\Projects\myapp $*
doskey logs=cd /D C:\Logs $*
myapp
echo %CD%

Output:

C:\Projects\myapp

Persist directory across cmd sessions with a startup script#

Set HKCU\Software\Microsoft\Command Processor\AutoRun to a path of a .cmd file that runs on every interactive cmd.exe. A typical entry:

reg add "HKCU\Software\Microsoft\Command Processor" /v AutoRun /t REG_SZ /d "%USERPROFILE%\cmd_init.cmd" /f

Output:

The operation completed successfully.

Then put cd /D C:\Projects\myapp (and any doskey macros) inside cmd_init.cmd.

Walk an entire workspace, run a command in each subdirectory#

for /d %d in (C:\Code\*) do (
    pushd "%d"
    echo --- %CD% ---
    git status -s
    popd
)

Output:

--- C:\Code\project1 ---
 M README.md
--- C:\Code\project2 ---
?? newfile.txt

PowerShell: jump to a Git repo’s root from any subdirectory#

function cdup-to-git {
    $loc = (git rev-parse --show-toplevel) 2>$null
    if ($LASTEXITCODE -eq 0) { Set-Location $loc }
    else { Write-Warning "Not inside a git repo" }
}
cdup-to-git

Output: (changes to repo root, or warns if not in one)

Open a folder graphically from the current cmd prompt#

explorer .

Output: (Explorer window opens at current directory)

Sources#

References consulted while writing this article. Links open in a new tab.

See also#

  • dir — list contents after navigating with cd.
  • pushd / popd — stack-based navigation; also the only cmd.exe way to use a UNC as working directory.
  • mkdir — create the directory before cd-ing into it.
  • Set-Location (PowerShell) — multi-provider navigation including registry and certificate stores.
  • doskey — define macros that wrap common cd patterns.
  • Linux cd, pwd, pushd/popd — Unix equivalents.