Why to use Push-Location instead of Set-Location
Find out why you would use Push-Location instead of Set-Location
May 7, 2017
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