Location stacks

We saw that push-location stores the current location and changes the location to the specified folder.  The location is stored on a stack. A stack is exactly what it sounds like. Think of a stack of plates. Each plate represents a location. The first location (plate) goes on the bottom and all subsequent locations (plates) pushed on to the stack and piled on top in order so that last stored location is on top.

We can see this by trying a number of push-locations.

Push-Location c:\test
Push-Location hklm:
Push-Location software\microsoft
Push-Location c:\teszzt2
Push-Location c:\scripts

 

We can then see the locations on the stack

PS> Get-Location -Stack

Path
----
C:\teszzt2
HKLM:\software\microsoft
HKLM:\
C:\test
C:\scripts

 

Stacks are unloaded from the top

PS> 1..5 | foreach {pop-location; get-location}

Path
----
C:\teszzt2
HKLM:\software\microsoft
HKLM:\
C:\test
C:\scripts

 

and now the stack is empty.

PS> Get-Location -Stack

 

it is possible to use the StackName parameter with push- & pop- location but that could get complicated and difficult to keep track of

Leave a Reply