HOWTO · PowerShell
How to Change the Working Directory in PowerShell
This article will show how to change your working directory in Windows PowerShell properly, and how it differs from Command Prompt syntax.
On this page
Use the PowerShell Set-Location cmdlet to change the current location. Its cd and chdir aliases work too, but the full cmdlet name is clearer in scripts and examples. A location can be a directory on the current drive or a path on another drive. The examples below use $HOME, which PowerShell defines as the current user’s home directory.
Change the Working Directory with Set-Location
Run this example to make the home directory the current location:
Set-Location -Path $HOME
Set-Location changes the current location for the PowerShell session. -Path names the destination; for a simple path it may be omitted, but spelling it out makes the command’s purpose clear. The command does not launch a new shell or change the location of other running programs. The supplied PowerShell run completed successfully and produced no stdout for this example.
Change Drives and Directories
PowerShell can switch to a directory on another drive in one Set-Location command. For example, Set-Location -Path 'D:\Work' selects that path when the D: drive and directory are available in the current PowerShell environment. A drive can be a file-system drive such as C:, or another drive exposed by a PowerShell provider; the destination must exist and be accessible.
Command Prompt handles the drive switch differently. Running cd D:\Work while the active drive is C: changes the remembered directory for D:, but does not make D: the active drive. In Command Prompt, switch with D: and then change its directory with cd \Work. This distinction is specific to Command Prompt; in PowerShell, use Set-Location with the destination path to change both the drive and directory together. These Command Prompt forms are described inline because this article’s runnable examples target PowerShell.
Handle Paths with Spaces
Quote a path when any directory name contains spaces so PowerShell treats the path as one argument. Both single and double quotes work for a literal path. For example, use Set-Location -Path 'C:\Program Files' or Set-Location -Path "C:\Program Files". Single quotes are convenient when the path should be treated literally; double quotes also allow PowerShell variable expansion inside the string. See the path with spaces guide for more detail. Replace the sample path with a directory that exists on your system.
Capture the New Location with -PassThru
Normally Set-Location changes the session location without returning a location object. Add -PassThru when the next command needs that object, for example to read its Path property or store it in a variable. This can avoid calling Get-Location separately when the object is useful immediately.
$newLocation = Set-Location -Path $HOME -PassThru
$newLocation.Path
/root
The second line reads the path from the returned location object. In the supplied PowerShell run, this example printed the following path. Because $HOME depends on the current user and system, your path may differ. -PassThru is optional: use Get-Location whenever you want to inspect the current location independently, and do not add -PassThru unless the returned object is useful to subsequent commands.
Use Set-Location Aliases
PowerShell defines cd and chdir as aliases for Set-Location; sl is another alias. For interactive work, the short forms can be convenient. The full cmdlet name is usually easier for someone else to recognize when reading a script, so treat sl as optional shorthand rather than a separate command with different behavior. All of these forms change the current PowerShell location and accept the same destination paths, including quoted paths with spaces.