Why to use Push-Location instead of Set-Location
Find out why you would use Push-Location instead of Set-Location
May 7, 2017
![Why to use Push-Location instead of Set-Location Why to use Push-Location instead of Set-Location](https://eu-images.contentstack.com/v3/assets/blt07f68461ccd75245/bltb8883b9cf05adbbd/661cf35af5d93092f95d5d48/world-1264062960720_0_4.jpg?width=1280&auto=webp&quality=95&format=jpg&disable=upscale)
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
You May Also Like