- What is Ansible Galaxy? - Fri, Oct 14 2022
- Pushing and popping: Navigating in PowerShell with Push-Location and Pop-Location - Wed, Mar 3 2021
Command line tools such as cd and chdir have been the gold standard for navigating file systems for decades. However, these tools have their limitations and are holding you back. The cd command will only display the directory you are currently in or change the directory you are currently in. For example, try using the cd command to navigate through the Windows registry. Remembering and typing long file system paths can present challenges each time you want to change locations.
Pushing and popping directories
Imagine for a moment that you have some LEGO blocks. You can push blocks together to create a stack of blocks. Then you decide to change the stack because you do not like the color or size of one of the blocks. You pop a LEGO block off the stack until you get to the offending color or size. You can have more than one stack of blocks. Some may be 2 × 2 blocks, and some may be 2 x 3. Each stack, however, can be pushed together or popped apart.
Pushing or popping in terms of navigating locations on your file system works the same way in PowerShell. You can add locations to a stack just as you would add two LEGO blocks together. This helps you by reducing the amount of brain energy you must waste remembering and typing paths as you move around the file system.
Push-Location explained
The Push-Location cmdlet pushes a location onto the location stack. The new location is now at the top of the stack. You can continue adding locations to the stack as necessary.
Pop-Location explained
The Pop-Location cmdlet pops the current location off the location stack. When a location is popped, you are returned to the previous location in the stack.
The difference between cd and Push/Pop-Location
At first, using Push-Location and Pop-Location may seem like using the cd command to navigate to a location. To some extent, it is. However, these two cmdlets provide additional value that cd does not provide.
The cd command will display the name of the current directory. When you use cd followed by a drive path, you move to that location. You can move back to the previous location using two dots (cd ..). You are limited to file system locations and a single location stack.
Push-Location and Pop-Location are not constrained to just the file system. Now you can use different providers available as PSdrives. That means you can navigate through many types of data structures. Moving through the Windows registry is just as easy as moving through a file system.
Examples
In this first example, use the Push-Location cmdlet to navigate to the location 'C:\Windows\' to the current default stack:
# Push a location onto the stack Push-Location -Path 'C:\Windows\' # Get the default location stack Get-Location -Stack
As you will see in the screenshot below, you have pushed the current location to the bottom of the stack and pushed 'C:\Windows\' onto the top of the location stack:
To return to the prior location, 'C:\Users\username\' in the default stack, use the Pop-Location cmdlet:
# Return to previous location in default stack Pop-Location
As you will see in the next screenshot, you have popped the current location from the top of the stack and returned to the previous location, 'C:\Users\username':
Working with the registry provider is like working with the file system provider. In this next example, use the same commands, Push-Location and Pop-Location, as before. This time, use two registry paths:
# Push a location onto the stack - registry # First path, HKEY_LOCAL_MACHINE Push-Location -Path HKLM:\System\CurrentControlSet\Control\BitlockerStatus # Second path, HKEY_CURRENT_USER Push-Location -Path HKCU:\Environment\ # Get the default location stack Get-Location -Stack
In the screenshot below, you will see that you now have used two providers to build a location stack of Filesystem and Registry:
What you have been working with so far is the default location stack. Now let's use StackNames to create separate location stacks.
In this next example, you create a couple of location arrays, Stack1 and Stack2:
# Create two arrays with different provider paths $Stack1 = @('C:\Temp\','C:\Windows\','C:\Users\') $Stack2 = @('HKLM:\System\CurrentControlSet\Control\BitlockerStatus','HKCU:\Environment\')
Now, using a foreach loop, create two named location stacks, as shown below:
# Create two named location stacks using foreach loop foreach ($location in $Stack1) { Push-Location -Path $location -StackName 'Stack1' } foreach ($location in $Stack2) { Push-Location -Path $location -StackName 'Stack2' }
You can now use the Set-Location cmdlet with the -StackName parameter to switch between these named location stacks, as shown below:
This is where the Push-Location and Pop-Location cmdlets really shine. You can create multiple named location stacks and easily switch between them using the Set-Location cmdlet. After switching to a named location stack, you use the Pop-Location cmdlet to navigate in reverse down the stack.
Subscribe to 4sysops newsletter!
What's next
The cd command can still be useful as an alias for the Set-Location cmdlet. Try using it to cycle through the location history in your PowerShell terminal session. Or maybe get the values of registry entries using Push-Location with a few other PowerShell cmdlets. The more you use the Push/Pop cmdlets to navigate data structures, the more you will appreciate not having to use the cd command.