hit counter

Use PowerShell to navigate the Windows folder structure

PowerShell uses several commands to navigate the Windows folder structure.

Normally, when you open a new PowerShell window, PowerShell starts you in your user profile directory. For example, in Figure 1, you can see that the PowerShell prompt points to C:\Users\Brien. Although the user profile has its place, PowerShell operations often require you to navigate to a different location within the folder hierarchy. In my case most of mine PowerShell scripts are located in a folder called C:\Scripts and are not accessible from the user profile folder.

Brien Posey\Users\Brien

Illustration 1. PowerShell often opens with your user profile folder.

In this article I will explain how to use PowerShell to navigate the Windows folder structure.

change drives

With PowerShell-based navigation, the first thing you need to know is how to switch to other drives.

To change drives, just type the drive letter you want to switch to, followed by a colon. For example, in Figure 2, I switched from the C: drive to the Q: drive by typing Q:. I then switched back to the C: drive by typing in C:.

Brien Posey    journey

figure 2 You can switch drives by typing the drive letter and a colon.

CD.. and CD\ commands

Most file system navigation involves traversing the directory structure. If you want to go down one level in the directory hierarchy, you can do so by typing CD.. (note there are two dots).

If you look again at the previous two images, you can see that PowerShell originally placed me in the C:\Users\Brien folder. By typing CD.., PowerShell would put me in the C:\Users folder.

Although you can use the CD.. command to move through the folder hierarchy level by level, it is not always the most efficient method. For example, if I were in the C:\Users\Brien folder and needed to go to the root folder, I could type the CD.. command twice, as I did in Figure 3.

Brien PoseyThe PowerShell screen shows using the CD.. command to go to the root folder

figure 3 Entering CD.. takes you down one level in the folder hierarchy.

However, as a shortcut, I could enter the CD\ command. That would take me to the root folder immediately. Figure 4 shows an example.

Brien PoseyThe PowerShell screen shows using CD\ to go to the root directory

figure 4 Typing CD\ causes PowerShell to go to the root directory.

As you can see, you can use the CD.. or CD\ command to move to a lower level within the folder hierarchy. You can also use the CD command to enter a folder. Just type CD followed by the folder name. If I’m in the C:\Users folder and want to navigate to the Brien subfolder, I could type CD Brien.

PowerShell aliases

Note that although this article covers the CD command, CD is not a “real” PowerShell command. In the days of DOS, CD was the command for traversing the directory structure. Microsoft included support for the CD command in PowerShell, both as a shortcut and as a way to make PowerShell a bit more like DOS.

The CD command is what is known as a alias.

In PowerShell, an alias is essentially just a shortcut. It is a shortened command that replaces a longer command. The longer command (which PowerShell calls as a cmdlet) that CD is an alias for is Set-Location.

The Set-Location cmdlet works the same as the CD command, with one small limitation. Unlike the CD command, you must add a space after the Set-Location cmdlet. While CD.. is a valid command, Set-Location.. is not a valid command. To avoid an error message, you would have to type the following instead:

Set-Location ..

You can see this in Figure 5.

Brien PoseyThe PowerShell screen shows the use of the Set-Location command.

Figure 5. The CD command is an alias for set location.

Get-ChildItem and DIR commands

There is another PowerShell cmdlet that when navigating in the Windows folder structure: Get-ChildItem.

That Get-ChildItem cmdlet shows the contents of the current folder. It’s useful when you want to navigate to a subfolder but aren’t sure of the exact folder name.

By the way, the Get-ChildItem cmdlet also has an alias. The alias is DIR, which is also from DOS times. Back then, DIR was the DOS directory command. Figure 6 shows how the DIR command works.

Brien PoseyThe PowerShell screen shows the use of the DIR command

Figure 6. You can use the DIR command to display the contents of the current folder.

With that in mind, let’s say that I needed to navigate to the C:\Users\Brien\Desktop folder, but couldn’t remember the name of the Desktop folder. I could navigate to C:\Users\Brien and then use either the DIR or the Get-ChildItem cmdlet to see a list of all the files and folders in that location. I could then use that information to get the name of the folder I need (Desktop in this case) and navigate to it with the CD command.

Leave a Comment