At PSConfEU, somebody asked me if I used Pop-Location and Push-Location. Well, I know it’s there, but no. 🙂 In this blog post, I will show you how to use it and that it might come in handy!
What is Pop-Location?
“The Pop-Location cmdlet changes the current location to the location most recently pushed onto the stack by using the Push-Location cmdlet. You can pop a location from the default stack or from a stack that you create by using a Push-Location command.”
What is Push-Location?
“The Push-Location cmdlet adds (“pushes”) the current location onto a location stack. If you specify a path, Push-Location pushes the current location onto a location stack and then changes the current location to the location specified by the path. You can use the Pop-Location cmdlet to get locations from the location stack.
By default, the Push-Location cmdlet pushes the current location onto the current location stack, but you can use the StackName parameter to specify an alternate location stack. If the stack does not exist, Push-Location creates it.”
How to use
Before showing you how to use it, you must know how Stacks work. You can use Push-Location to store different places, which you can switch to using Pop-Location. You can store things (Filesystem, Registry, Variables, Functions, Certificates, etc.) in the Default location stack like this:

You can see it switched from C:\Users\HarmVeenstra to C:\Data\GitHub\Powershellisfun\, and then it switched from that location to C:\Scipts. When using Pop-Location, it will jump back to the previous locations:

In the example above, you can see that it jumped back to the previous location and again until it could not find another previous location. It will throw the error in red that it can’t find anything in the location stack anymore.
You can also create named stacks containing specific locations like this:

In the example above, I added C:\Data\GitHub\Powershellisfun, C:\Scripts, and C:\Data to the Stack called PSIF (PowerShellIsFun), which you can retrieve using Get-Location -StackName PSIF.
Because you have a StackName configured, you can use that in a For-Each loop to walk through them and do something in each location. For Example:

In the example above, I stored the StackName PSIF in $stack, looped through the locations, and output the folder name as a screen showing which Push-Location was used.
You can also create multiple sets of locations in different stacks and use Set-Location to switch from one to another like this:

In the example above, I created a $stack1 and $stack2 variable containing three filesystem locations. Then, I added them using a For-Each loop to the StackName Stack1 and Stack2 and used Get-Location to show the locations stored in each StackName. You can then use Set-Location -StackName stack1 or stack2 to switch to that StackName in your script if needed.
Now that we know how to stack locations in a StackName… How can we use that? In the example below, I switch through all the locations stored in $stack1:

But why is the red error stating that stack1 can’t be found?! The stack is empty and gone once you loop through all the locations. You have to set it again as we did above in the For-Each loop. (Or by using Push-Location -Path xyz -StackName stack1 for each location you want to add)
Why not use Set-Location or CD?
Set-Location is an alias of CD (Change Directory). Why not use CD / Set-Location with absolute paths instead of Push-Location? Yes, this is what I usually do, but in some cases, storing your current directory using Push-Location is safer to ensure you can return to that specific path using Pop-Location. This is beneficial when running other scripts inside your scripts or calling external programs. If they change your path after execution, the rest of your script might not work anymore.
Wrapping up
In the chapters above, I showed you how to use Pop-Location/Push-Location and why to use it. It might not fit your everyday use, but it might come in handy, and it’s something you can put in your utility belt 😉 Have a lovely weekend!