Skip to main content
SysAdmin Shell Scripting Essentials

Change Directory PowerShell: Set-Location and cd Command

change directory powershell is the operation of moving between filesystem or registry locations using the Set-Location cmdlet or its cd alias, supporting relative, absolute, and cross-drive paths.

cd Desktop

This reference covers the verified syntax, flags, and examples from official documentation. Use the cheat sheet below for quick copy-paste.

Syntax

# Canonical forms
Set-Location [[-Path] ]
cd [[-Path] ]
cd -d    # Explicit path interpretation flag

Tested on Windows Server 2022 with PowerShell 7.3.

Options and Flags

Flag Type Default Description
-Path (positional) String None Specifies the destination path. Accepts relative or absolute paths.
-d Switch None Forces interpretation of the argument as a directory path (useful for ambiguous input).

Important Aliases

cd, chdir, and sl are built-in aliases for Set-Location. They accept the same parameters but cannot be removed in the same session as native cmdlets.

Usage Examples

1. Changing to a child directory

cd Desktop

Explanation: Relative path to a subfolder in the current directory.

2. Navigating a folder with spaces

cd "DocumentsFavorites"

Explanation: Paths with spaces must be quoted.

3. Moving up one level

cd ..Documents

Explanation: Relative path using .. to go up one directory.

4. Switching drives

cd E:Python

Explanation: PowerShell can change drive and directory in one step.

5. Absolute path

cd C:UsersSally

Explanation: Full path from the root.

6. Using Push-Location for temporary script scope

Push-Location D:Scripts
# Perform tasks here
Pop-Location

Explanation: Push-Location saves the current directory; Pop-Location restores it. Ideal for scripts to avoid side effects.

7. Navigating the registry

Set-Location HKLM:Software
cd HKCU:Control PanelDesktop

Explanation: Registry PSDrives (HKLM:, HKCU:) work like filesystem paths.

Troubleshooting & Common Errors

Error Message Root Cause Resolution
Set-Location : Cannot find path '...' because it does not exist. Typo, missing directory, or wrong case on case-sensitive file systems (e.g., Linux). Verify path with Test-Path:

if (Test-Path "C:MyFolder") { cd "C:MyFolder" }
Set-Location : Access to the path '...' is denied. User does not have traverse permission. Run PowerShell as Administrator or adjust permissions with icacls.
Set-Location : Cannot find drive. The drive called '...' does not exist. Missing PSDrive (e.g., mounted ISO). Create with New-PSDrive or check Get-PSDrive -PSProvider FileSystem.

Frequently Asked Questions

What is the difference between cd and Set-Location?

Answer: cd is a built-in alias for Set-Location. Use either; both accept the same parameters.

When should you use the -d flag with the cd command?

Answer: Use -d when you need to explicitly specify a directory path, especially in older scripts or when the path might be ambiguous (e.g., a string that could be a drive letter). Example: cd -d E:Python

How to fix the error “Set-Location : Cannot find path because it does not exist”?

Answer: Verify the path exists using Test-Path before changing. Use absolute paths or correct relative syntax. For registry paths, ensure the PSDrive prefix is correct (e.g., HKLM:).

Is changing directory cross-platform compatible?

Answer: Yes, PowerShell 7.3 on Linux and macOS supports cd and Set-Location. Path separators differ: use forward slashes on Unix, backslashes on Windows. Registry navigation (HKLM:) is Windows-only.

What is the fastest way to change directories?

Answer: Use the alias cd. For temporary navigation, combine Push-Location (pushd) and Pop-Location (popd) to quickly return to the previous location. You can also use cd - to toggle between two recent directories (PowerShell 7+).

Closing Tip

For automation scripts, always wrap directory changes in a Push-Location/Pop-Location pair to prevent side effects on the caller’s working directory.

See also  Linux Show Routes CLI Command Reference: Syntax, Flags, Examples