Using Named Location Stacks in PowerShell
Summary: Microsoft Scripting Guy, Ed Wilson, talks about using named location stacks in Windows PowerShell.
Hey, Scripting Guy! If I need to move around in two types of locations, how would I do it? Suppose I need to look in various places in my profile, and then I need to do some work in data locations elsewhere on my hard drive. How would I be able to keep track of the different locations?
Microsoft Scripting Guy, Ed Wilson, is here. Today is the last day of the Windows PowerShell Summit in Charlotte. In fact, tonight the Charlotte Windows PowerShell User Group is having a special meeting to celebrate. We have invited Lee Holmes and Paul Higinbotham from the Windows PowerShell team to talk about their favorite Windows PowerShell tips and tricks. This will be a highly interactive session, and there are only a few seats left for this free event. To register for this unique opportunity, go to Everyday PowerShell – My Favorite PowerShell Hacks with Lee Holmes.
One of the things that makes navigating around in the Windows PowerShell console or prompt a bit annoying is typing the long path names. Of course, Tab expansion helps this task, but it only expands subdirectory by subdirectory, and therefore, for deeply nested folders, there is still a lot of stopping and starting.
Of course, using a mouse in File Explorer is not necessarily any faster. In fact, it usually is not due to the amount of time that it takes for thousands of files to scroll into the pane each time one clicks a new subfolder. With the Back button in File Explorer, this task became a bit easier. So, where is the Back button in the Windows PowerShell console? Well, this is where location stacks can come into play.
Note This post continues yesterday's Hey, Scripting Guy! Blog post, Location, Location, Location in PowerShell. If you have not read that one yet, you should before you continue reading today’s post.
If I am working in multiple locations, and I can logically group them, then I like to create named locations. This is easily done by supplying a name for the stack when I call the Push-Location (PushD is the alias) cmdlet. If the stack name does not exist, it is created. If the stack name does exist, the new location is added to that particular named stack.
In the following example, I am working with a bunch of folders that are all named FSO in some fashion. So I create a new stack named FSO, and add the locations to that stack.
When you use Push-Location, the location being navigated to is added to the location stack, then the current location changes to the path specified. In the following examples, I begin at drive C, and navigate to the C:\fso folder. My current location changes to C:\fso and the path is stored in a location stack named FSO. I then change my current location to C:\fso1, and once again, add the path to the location stack named FSO. I again change my location to C:\fso2, and I also add that location to the stack. Here are the commands and the associated output:
PS C:\> pushd -StackName fso -Path C:\fso
PS C:\fso> pushd -StackName fso -Path C:\fso1
PS C:\fso1> pushd -StackName fso -Path C:\fso2
Because I have not used Pop-Location (PopD is the alias), all three locations are stored on the FSO stack. Now I want to navigate to a few locations in my profile. I also want to store those locations, but I want them to be accessible via a different name. So I create a new location stack named Ed. Once again, I navigate to the different locations by using PushD. Here are the commands and the output:
PS C:\fso2> pushd -StackName ed -Path C:\Users\ed\Documents
PS C:\Users\ed\Documents> pushd -StackName ed -Path C:\Users\ed\Downloads
PS C:\Users\ed\Downloads> pushd -StackName ed -Path C:\Users\ed\Pictures
I have added six locations to two differently named stacks. If I use Get-Location –Stack, but I do not specify any names, I will retrieve the default location stack. This command is shown here:
PS C:\> Get-Location -Stack
Because all six of my locations are on named stacks, nothing returns from the command. I then use the Up arrow, and try to look at the FSO stack. The command generates an error message. This command and its output are shown here:
The reason the error occurs is that the –Stack parameter is used for the default stack only. I cannot specify a name here. Instead, I need to use the –StackName parameter. When I do this, I can retrieve the locations as shown here:
PS C:\> Get-Location -StackName fso
As shown here, I can also retrieve locations from the Ed stack:
PS C:\fso2> Get-Location -StackName ed
When it comes time to revisit the locations, I use Set-Location to change my default location stack name. Then I can use PopD to move through the directories. This command is shown here:
PS C:\> Set-Location -StackName fso
PS C:\> popd
PS C:\> Get-Location -Stack
PS C:\> popd
PS C:\> popd
PS C:\fso1> popd
PS C:\fso> popd
When I reach the end of the stack, I change to my new stack by again calling the Set-Location cmdlet and then walking through those locations by using Pop-Location (PopD):
PS C:\Users\ed\Downloads> Set-Location -StackName ed
PS C:\Users\ed\Downloads> Get-Location -Stack
PS C:\Users\ed\Downloads> popd
AK, that is all there is to using named locations in Windows PowerShell. Provider Week will continue tomorrow when I will talk about more cool stuff.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy