Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Set-Location for an ordinary move to another PowerShell location. Use Push-Location when you want to save where you are first, so you can return later with Pop-Location. The difference is whether PowerShell keeps a return point on a location stack.

What each command does

Set-Location changes the current working location to the one you specify; it does not, by itself, save your starting location on the location stack. Push-Location saves the current location on a stack and then changes to the requested location. Pop-Location retrieves a saved location. Microsoft describes Push-Location as adding the current location to the top of a location stack and Set-Location as setting the current working location to a specified location.

That makes Push-Location useful for a temporary detour, especially when you may want to return without typing your original path again. For a normal navigation change where you do not need a stack-based return, Set-Location is the direct choice.

Choose the command for the job

Need Command Effect
Move somewhere without saving a return point on the location stack Set-Location <path> Changes the current working location.
Save the current location, then move Push-Location <path> Pushes the current location onto the stack, then changes to the target.
Save the current location without moving Push-Location Adds the current location to the stack; the location stays the same.
Return to a saved location Pop-Location Retrieves a location from the stack.
Navigate recent locations interactively Set-Location -Path - or Set-Location -Path + Uses recent-location history, which is separate from the explicit location stack.

Use Push-Location for a temporary detour

For an interactive session, push the current location before visiting another one, then pop when you are ready to return:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Push-Location C:Windows
# inspect something
Pop-Location

For a script, a try/finally block is a robust way to ensure the saved location is restored after the work finishes, including when a command in the block encounters an error:

Push-Location -Path 'C:Program FilesPowerShell'
try {
    # Run commands that should use this location
}
finally {
    Pop-Location
}

By contrast, an ordinary move needs no push/pop pair:

Set-Location -Path 'C:Program FilesPowerShell'

Nested detours unwind in reverse order

Location stacks are last-in, first-out. Each Pop-Location returns to the most recently saved location, so nested visits unwind in reverse order:

Push-Location C:Windows
Push-Location C:Temp
Pop-Location # returns to C:Windows
Pop-Location # returns to the location from before C:Windows

This is useful when a task temporarily moves through several locations and needs to retrace them in order. A single Set-Location does not create those stack entries.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #3
Sale
PowerShell for Sysadmins: Workflow Automation Made Easy
  • Book - powershell for sysadmins: workflow automation made easy
  • Language: english
  • Binding: paperback

Named location stacks

PowerShell uses an unnamed location stack by default. You can create and use a named stack with Push-Location -StackName, then return from that stack with Pop-Location -StackName. For example:

Push-Location -Path HKLM:SOFTWARE -StackName Registry
# Work in the registry provider
Pop-Location -StackName Registry

Set-Location -StackName <name> selects a named stack as the current stack; it does not change the current location. When a named stack is current, ordinary current-stack operations do not fully access the unnamed default stack. To select the unnamed stack again, use a null or empty stack name, as described in the Set-Location reference.

These commands work beyond filesystem directories

PowerShell locations come from providers, so they can include filesystem paths, registry paths, and other provider locations—not only folders on a drive. The Push-Location reference includes examples using both filesystem and registry locations.

For shorter commands, pushd is an alias for Push-Location. Set-Location has the aliases cd, chdir, and sl. The Managing current location guide covers location workflows and aliases.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Set-Location’s recent-location history is separate

The PowerShell 7.4 Set-Location documentation says PowerShell 6.2 added support for - and + as -Path values. These values navigate a history of the last 20 locations. That history is separate from the explicit location stack used by Push-Location and Pop-Location; check the reference for the PowerShell version you use if this behavior matters to your workflow.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.