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 is pointing 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 my PowerShell scripts are located in a folder called C:\Scripts and are inaccessible from the user profile folder.
Figure 1. PowerShell often opens to your user profile folder.
In this article, I will explain how to navigate the Windows folder structure using PowerShell.
In PowerShell-based navigation, the first thing to know is how to switch to different drives.
To change drives, just enter the drive letter you want to go to, followed by a colon. In Figure 2, for example, I switched from the C: drive to the Q: drive by typing Q:. I then switched back to the C: drive by typing C:.
Figure 2. You can switch drives by entering the drive letter and a colon.
CD.. and CD\ Commands
Most file system navigation involves traversing the directory structure. If you want to drop down one level within the directory hierarchy, you can do so by typing CD.. (note that there are two periods).
If you look back at the previous two figures, you can see that PowerShell initially placed me in the C:\Users\Brien folder. By entering CD.., PowerShell would drop me down to the C:\Users folder.
Although you can use the CD.. command to move through the folder hierarchy one level at a time, it’s not always the most efficient method. For example, if I were in the C:\Users\Brien folder and needed to drop down to the root folder, I could enter the CD.. command twice, as I have done in Figure 3.
Figure 3. Typing CD.. moves you down a level in the folder hierarchy.
However, as a shortcut, I could enter the CD\ command. That would immediately drop me down to the root folder. Figure 4 provides an example.
Figure 4. Typing CD\ causes PowerShell to move 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 were in the C:\Users folder and wanted to navigate to the Brien subfolder, I could type CD Brien.
Note that even though this article discusses the CD command, CD is not a “real” PowerShell command. 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 way of making PowerShell a bit more like DOS.
The CD command is what is known as an alias.
In PowerShell, an alias is essentially just a shortcut. It’s a shortened command that takes the place of a longer command. The longer command (which PowerShell calls a cmdlet) for which CD is an alias is Set-Location.
The Set-Location cmdlet works identically to the CD command, with one minor caveat. Unlike the CD command, you must include a space after the Set-Location cmdlet. Whereas CD.. is a valid command, Set-Location.. is not. Instead, to avoid receiving an error, you would have to type:
You can see this in Figure 5.
Figure 5. The CD command is an alias for Set-Location.
Get-ChildItem and DIR Commands
There is one more PowerShell cmdlet that is useful when navigating the Windows folder structure: Get-ChildItem.
The Get-ChildItem cmdlet displays the contents of the current folder. It’s helpful if you want to navigate to a subfolder but aren’t sure of the exact folder name.
Incidentally, the Get-ChildItem cmdlet also has an alias. The alias is DIR, which also originates from the days of DOS. Back then, 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 current folder’s contents.
So, with that in mind, suppose 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, then use either the DIR or the Get-ChildItem cmdlet to see a list of all of the files and folders in that location. I could then use that information to get the name of the folder that I need (in this case, Desktop) and navigate to it using the CD command.