Mastering Windows File System Navigation with PowerShell

Brien Posey demonstrates how to navigate the Windows file system using PowerShell.

In this tutorial video, PowerShell expert Brien Posey explains how to use PowerShell to navigate the Windows file system. The video explains two different ways to navigate the file system: one is by using commands inherited from DOS, and the other is by using PowerShell cmdlets.

The tutorial covers creating folders, accessing subfolders, dropping down multiple levels, and removing folders.

The below transcript is lightly edited for length and clarity.

 

Transcript:

Brien Posey: Hello, greetings, and welcome. I'm Brien Posey. In this video, I want to 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 different ways to do it. The reason why there are two different ways has to do with the history of Windows. Long before Microsoft ever created Windows, there was another Microsoft operating system called DOS, or Disk Operating System. Now, DOS was a very primitive operating system, and it was entirely command-line based. As you can imagine, there were commands that existed way back then for navigating the file system. A lot of those commands still exist in Windows today purely for backward compatibility reasons.

Examples of DOS Commands in PowerShell

Let me show you what I'm talking about. To start, rather than opening up PowerShell, I'm going to open up the Windows Command Prompt. To do that, I'm going to right-click on the Start button, I'll go to Run, and I'll type cmd, and click OK. This 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.

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 go back to the Brien folder, I can type cd Brien. So, that's just a quick example of how we used to navigate the file system in the days of DOS.

Now, a lot 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. But I would say 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. So, here we are in a PowerShell environment. The first thing that I'm going to do is exactly what we did a moment ago, just 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 or directory’s contents.

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

I just want to show you a few DOS-style commands that you can use to navigate the file system. I already showed you the dir command. The next thing that I want to show you is how to create a folder or a directory in the style of DOS. The way that you would do that is using the md command. That stood for Make Directory. So, I'll type md and then space, then the name of the folder that you want to create. In this case, I'll create a folder called Folder1. And 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 once again, and type cd and then Folder1. And there I am.

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

Now, I mentioned earlier that PowerShell has adopted a lot of the navigational commands that existed in 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.. . With that said, let me 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.. and 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 and do that multiple times, just as we've been doing. You can also type cd\  and press enter, and that will drop you down to the root folder.

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 what I would do is I would type rd, which stands for Remove Directory, and then the name of the folder and press Enter. And now 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 gone as well.

So, that's just a quick demonstration of the DOS way of doing things in PowerShell.

PowerShell Aliases Explained

So, what about PowerShell-style navigation? All the DOS-style commands that I've 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 you can use the Get-Item cmdlet to find the actual PowerShell cmdlet that corresponds to various aliases. Let me show you how that works. One of the commands that we've used a lot so far in this video is cd, but cd is actually an alias. If I wanted to find out what the PowerShell equivalent to the cd command is, I could type Get-Item, then cd, and then press enter. What we see is that cd is an alias for the PowerShell Set-Location cmdlet.

Let's look at a few more. 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 look at a couple more. This time, I'll type Get-Item and md (the command that we use to create a folder). 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.

File System Navigation via PowerShell Cmdlets

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

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

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

The first thing that I'm going to do is create a folder called Folder1, just as we did before. The way that I do that is by using 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 then the name of the folder and press Enter. And now we're in that folder that we just created.

If I want to drop down to the root directory, then what we can do is type Set-Location, followed by the backslash. When we use the cd command, the backslash was right up against the cd. But being that we're using Set-Location, we have to have 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'm going to type Set-Location Folder1, and then I'm going to 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, then I would use the Set-Location command once again, and I'll 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 .. 

I'll 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. The way that we do that is to 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. The way that we do that is with 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, what I can do is type Set-Location \. That's going to take us to the root folder. I'll repeat the Get-ChildItem cmdlet. Now we can 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.

So, those are just some techniques that you can use to navigate the Windows file system using PowerShell.

Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish