Use PowerShell to Navigate the Windows Folder Structure
PowerShell uses several commands to navigate the Windows folder structure.
When you open a new PowerShell window, PowerShell usually starts you in your user profile directory. In Figure 1, for example, you can see that the PowerShell prompt points to C:UsersBrien. Although the user profile has its place, PowerShell operations often require you to navigate to a different location in the folder hierarchy. In my case, most of my PowerShell Scripts are located in a folder called C:Scripts and cannot be accessed from the user profile folder.
Figure 1. PowerShell often opens in your user profile folder.
In this article, I’ll explain how to navigate the Windows folder structure using PowerShell.
Change of player
In PowerShell-based navigation, the first thing to know is how to switch to different drives.
To change drives, simply enter the letter of the drive you want to access, followed by a colon. In Figure 2, for example, I went from the C: drive to the Q: drive by typing Q:. I then returned to the C: drive by typing C:.
Picture 2. You can change drives by typing the drive letter followed by 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 that there are two dots).
If you look at the previous two figures, you can see that PowerShell initially placed me in the C:UsersBrien folder. Entering CD .., PowerShell would drop me into the C:Users folder.
Although you can use the CD.. command to navigate the folder hierarchy one level at a time, it is not always the most efficient method. For example, if I was in the C:UsersBrien folder and needed to go down to the root folder, I could enter the command CD.. twice, as I did in Figure 3.
Picture 3. Typing CD.. takes you down one level in the folder hierarchy.
However, as a shortcut, I could enter the command CD. This would immediately drop me into the root folder. Figure 4 gives an example.
Figure 4. If you type CD, PowerShell moves to the root directory.
As you can see, you can use the CD.. or CD command to move down one level in the folder hierarchy. You can also use the CD command to enter a folder. Just type CD, followed by the folder name. If I was in the C:Users folder and wanted to access the Brien subfolder, I could type CD Brien.
Note that although this article discusses the CD command, CD is not a “real” PowerShell command. Back in the days of DOS, CD was the command used to traverse the directory structure. Microsoft has included support for the CD command in PowerShell both as a shortcut and as a way to make PowerShell a little closer to DOS.
The CD command is what is known as a a.k.a.
In PowerShell, an alias is basically a shortcut. It is a shortened command that replaces a longer command. The longest command (which PowerShell calls a cmdlet) for which CD is an alias is Set-Location.
The Set-Location cmdlet works similarly to the CD command, with one minor caveat. Unlike the CD command, you must include a space after the Set-Location cmdlet. While CD.. is a valid command, Set-Location.. is not. Instead, to avoid receiving an error, you’ll need to type:
You can see it in figure 5.
Figure 5. The CD command is an alias for Set-Location.
Get-ChildItem and DIR Commands
There is another PowerShell cmdlet that is useful when navigating through the Windows folder structure: Get-ChildItem.
The Get-ChildItem cmdlet displays the contents of the current folder. This is useful if you want to access a subfolder but are unsure of the exact folder name.
Incidentally, the Get-ChildItem cmdlet also has an alias. The alias is DIR, which also dates back to the days of DOS. At the time, DIR was the DOS directory command. You can see how the DIR command works in Figure 6.
Figure 6. You can use the DIR command to see the contents of the current folder.
So with that in mind, suppose I need to access the C:UsersBrienDesktop folder but I can’t remember the name of the Desktop folder. I could navigate to C:UsersBrien, then use the DIR or the Get-ChildItem cmdlet to see a list of all files and folders in that location. I could then use this information to get the name of the folder I need (in this case Desktop) and navigate to it using the CD command.