Skip navigation
Why to use Push-Location instead of Set-Location

Why to use Push-Location instead of Set-Location

Q. Why would I use Push-Location instead of Set-Location?

A. Ordinarily to change location the Set-Location cmdlet is used however consider you want to change location to perform some action and then return back to the original location. You could use something like:

$loc = Get-Location
Set-Location -Path D:\Temp
Get-Location
Set-Location -Path $loc

However Push-Location does this by placing your current location onto a stack which can then be restored using Pop-Location. For example:

Push-Location
Set-Location -Path D:\Temp
Get-Location
Pop-Location

For those used to pushd/popd this is the same thing but in PowerShell. This can be very useful then you need to move location to perform some action then return back.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish