With both cmdlet you can set the current working folder in your console.
However, Push-Location becomes really interesting when you associate its use with Pop-Location. Both cmdlets act merely like the Back and Forward buttons from the Windows File Explorer.
With Push-Location and Pop-Location, the history is named the stack.
However, unlike with the Windows File Explorer, with Pop-Location you cannot pick-up one location in the midst of the stack. The Push/Pop system works with the LIFO method:
- Every time you push another location, the former one is added to the top of the stack.
- Every time you pop a location, it is removed from the top of the stack.
As you can notice in the following example and screenshot:
- The C:\Windows\System32\LogFiles location has not been added to the stack because no other location has been pushed after it.
- The C:\Windows location was the latest to be added to the stack and also the first one to be popped out and remove from the stack.
Interestingly Push-Location and Pop-Location can handle multiple stacks.
As you can see in the following example, I filled 2 stacks and then I popped the latest location from one of both stacks.
In the next example and screenshot, you can notice that:
- I created and filed a stack named Windir and then I created an filed another stack named Programs.
- I popped the latest location from the first stack named Windir