Why to use Push-Location instead of Set-Location

Find out why you would use Push-Location instead of Set-Location

John Savill

May 7, 2017

1 Min Read
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.

About the Author

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like