Mastering Windows File System Navigation With PowerShell

Learn how to use PowerShell to navigate the Windows file system.

Brien Posey

May 23, 2023

In this tutorial, Brien Posey demonstrates two ways to navigate the file system using PowerShell:

  1. Commands inherited from DOS

  2. PowerShell cmdlets

The tutorial covers tasks like creating, accessing, and deleting folders.

Subscribe to ITPro Today's YouTube channel for PowerShell tutorials and more.

The below transcript is edited for clarity.

Transcript:

Brien Posey: In this tutorial, I'll show you how to use PowerShell to navigate the Windows file system.

Now, the interesting thing about using PowerShell for file system navigation is that there are two ways to do it: using older DOS-based commands and PowerShell cmdlets.

Before Windows, Microsoft used an operating system called DOS (Disk Operating System), which was entirely command-line-based. Many DOS commands for navigating the file system are still supported in PowerShell for backward compatibility.

Example: Using DOS commands

Let me show you what I'm talking about. To start, I will open the Windows Command Prompt (CMD) rather than PowerShell.

Right-click the Start button, select Run, type cmd, and press OK. Doing so opens the Windows Command Prompt window. So, we're in an entirely different environment that predates PowerShell, called the Windows Command Prompt. The vast majority of the DOS functionality exists in the Windows command prompt.

Related:PowerShell Error Handling: Learn These Key Techniques

cd command

For example, if we wanted to drop down to the Users folder, whereas right now we’re in the Users\Brien folder, we could type:

cd..

That would drop us down a level. If I wanted to return to the Brien folder, I could type cd Brien.

So, that's just a quick example of how we used to navigate the file system in the days of DOS.

Much of this DOS functionality still exists in PowerShell today. Now, everything in PowerShell doesn't work exactly as it did in the days of DOS. However, I would say that 90% to 95% of the DOS file system navigation capabilities still exist in PowerShell.

Let's see what that looks like. I'm going to go ahead and close out the Windows Command Prompt, and I'm going to switch over to PowerShell.

dir command

The first thing I will do is what we did a moment ago: drop down to a different level. I'll type cd.. . Then, the first DOS-type command that I want to show you besides cd is dir.

Now, if you're wondering what cd stood for and what dir stood for, well, in the days of DOS, a folder was known as a directory. The dir command is the Directory command that shows you the contents of the current folder.

So, let me type dir and press Enter. You can see the current folder's (or directory’s) contents.

Related:Tracking Windows Group Policy Changes With PowerShell

The cd command I've used a few times stands for "Change Directory." So, you can type cd.. and drop down to a lower-level directory. Let's go ahead and drop down to the root. I'll type cd.. .

md command

Next, I want to show you how to create a folder or a directory using the md ("Make Directory") command. So, I'll type md and a space, then the folder name that I want to create. In this case, I'll make a folder called Folder1. I'll press Enter. And that folder has been created.

So, if I wanted to navigate to that folder, I would use the cd (or change directory) command again, and type cd and then Folder1. And there I am.

We can also create subfolders in this way. If I wanted to create a subfolder called Folder2, I would type md, then the folder name (Folder2), and press Enter. I've created a folder called Folder2. If I wanted to move into that folder, I would type cd folder2.

rd command

Now, what about removing folders? Well, let me go ahead and go back up a folder level. So, I'll type cd folder1. Then, I'll enter the dir command to see what's in that folder, which we already know what's in that folder. But just for the sake of demonstration, I'll type dir. And we can see that this folder is empty except for one subfolder called Folder2. If I wanted to delete that folder, then I would type rd ("Remove Directory") and the folder name, then press Enter. And if I type the dir command again, we can see that that folder is gone. And if I drop down to the root directory and repeat the command on Folder1, that folder is also gone.

Related:PowerShell and AI: Create a Microsoft Word Copilot

Limitations of DOS Commands in PowerShell

I mentioned earlier that PowerShell has adopted many of the navigational commands from DOS, but that is not a 100% perfect representation. Let me show you what I mean.

As you've already seen, if you want to drop down a file level, you type cd.. . I will go back into Folder2 by typing cd folder2. Now, in the days of DOS, you could append extra periods to the cd command to drop down multiple levels of the file structure. If I wanted to drop down to the root, I could type cd.., then add an extra period (cd…). But this doesn't work in PowerShell. If I press Enter, I get an error message.

If you want to drop down multiple levels, there are two ways of doing it. You can type cd.. and press Enter several times, just as we've done. You can also type cd and press Enter that will drop you down to the root folder.

So, what about PowerShell-style navigation?

Aliases in PowerShell

All the DOS-style commands I have shown you exist as aliases to PowerShell cmdlets. Let's look at that.

PowerShell has a hidden drive called the Alias Drive. The way that you would access that alias drive is to type Set-Location and then Alias: . When I do that, you'll notice that the PowerShell prompt changes from C: to Alias: . So, now we're in PowerShell’s hidden alias drive.

I did that because I wanted to show you how to use the Get-Item cmdlet to find the actual PowerShell cmdlet corresponding to various aliases. Let me show you how that works.

One of the commands used frequently in this video is cd, but cd is an alias. If I wanted to find the PowerShell equivalent to the cd command, I could type Get-Item, then cd, and press enter. We see that cd is an alias for the PowerShell Set-Location cmdlet.

Another command that I've used is dir. If we type Get-Item followed by dir and press enter, you can see that dir is a PowerShell alias for the Get-ChildItem cmdlet.

Let's do one more. I'll type Get-Item and then rd (the remove directory command). We can see that rd is an alias for Remove-Item.

With that said, let's look at how to navigate the file system using only PowerShell cmdlets.

I'll do roughly the same thing I did using the DOS-style cmdlets.

So, let me switch back over to the C: drive. I do that by typing C: and pressing Enter. And so now, we're in the root folder of C:.

The first thing I will do is create a folder called Folder1, just as we did before. To do that, I will use the mkdir command, followed by the folder name. I'll press Enter, and the folder has been created. To access that folder, we need to use the Set-Location command. So, I'll type Set-Location and the folder name, then press Enter. And now we're in that folder that we just created.

If we want to drop down to the root directory, we can type Set-Location, followed by the backslash. When we used the cd command, the backslash was right up against the cd. But since we're using Set-Location, we must put a space before the backslash. So I'll type Set-Location and press Enter. That's going to drop us down to the root folder.

Let's go ahead and create another subfolder. I will type Set-Location Folder1 and then create a subfolder called Folder2. So, now I'm going to type mkdir and then Folder2. Folder2 has been created.

If I want to access Folder2, I use the Set-Location command again, and I change this from Folder1 to Folder2. And now we're in Folder2.

We can also use Set-Location with the two periods to drop down a folder level, just as we did with cd. But once again, we have to put a space in there. So, the way that we would do this is to type:

Set-Location ..

l press Enter. We've dropped down to Folder1.

Let's look at the process of removing a folder. I'm going to go ahead and delete Folder2. To do that, I use the Remove-Item cmdlet. Then, I'll type the name of the folder. The folder has been removed.

You'll notice that we don't get any visible output, so we need to check the contents of this folder. We do that by using the Get-ChildItem cmdlet. I'll type Get-ChildItem and press Enter. We can see that Folder1 is indeed empty.

If you want to see Get-ChildItem produce some visible results, you can type Set-Location. That's going to take us to the root folder. We will repeat the Get-ChildItem cmdlet to see the contents of the root folder. Let's go ahead and get rid of Folder1. To do that, I'm going to use Remove-Item Folder1. Folder1 has been deleted now. If we repeat the Get-ChildItem cmdlet, we can see that Folder1 no longer exists.

You can use these techniques to navigate the Windows file system using PowerShell.

About the Author

Brien Posey

Brien Posey is a bestselling technology author, a speaker, and a 20X Microsoft MVP. In addition to his ongoing work in IT, Posey has spent the last several years training as a commercial astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space.

http://brienposey.com/

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like