PowerShell for File Management (Part 1)

by [Published on 26 July 2016 / Last Updated on 26 July 2016]

This article series explains how to perform various file system tasks using PowerShell.

If you would like to read the other parts in this article series please go to:

I got my start in IT back in the days of DOS. For those who might not be familiar with the term, DOS stood for Disk Operating System. It was an entirely Command Line based operating system that was small enough to fit onto a floppy disk. Think about that one for a moment… DOS was a command line operating system that was specifically designed for performing disk related functions (after all, disk was in the name).

With DOS, it was possible to locate files based on various criteria, but you had to know what command to use. What’s interesting, is that Microsoft eventually added many of those commands to the Windows Command Prompt, and eventually into PowerShell. So what this really means is that even though PowerShell is only about ten years old, it contains commands that are left over from the 1980s. The bad news is that some of these commands are pretty archaic and can be tedious to use. The good news is that for the most part, those commands exist only for backward compatibility purposes. Microsoft has created PowerShell equivalents to many of the old DOS commands. Let me give you a really simple example.

In the days of DOS, the command that was used to display the current folder’s contents was DIR (which was short for directory). There were lots of variations to the DIR command. For example, if you wanted to display the contents of the current folder and all of the sub folders, you could append the /S switch. In any case, DIR was the basic command used for displaying folder contents or for searching for specific files. The DIR command still works in PowerShell, but Microsoft has created a PowerShell cmdlet that does something similar. That cmdlet is Get-ChildItem. You can see both commands being used in the image below.

Image
PowerShell supports both the DIR command and the Get-ChildItem cmdlet.

Of course this begs the question of why PowerShell contains two commands that do the same thing. I don’t know Microsoft’s official answer, but I suspect that it has to do with consistency. In the world of PowerShell, each cmdlet follows a very specific format. Cmdlets are made up of a verb and a noun. In the case of Get-ChildItem, Get is the verb and ChildItem is the noun. This syntax helps to make PowerShell cmdlets a bit more intuitive, and it is really nice that the entire native cmdlet set adheres to a rigid structure.

The introduction of PowerShell cmdlets as a replacement for the old DOS commands has also allowed for a degree of modernization. Microsoft is able to integrate functionality into the PowerShell cmdlets that simply did not exist in the days of DOS. So with that being the case, I thought that it might be fun to show you some of the things that you can do by using the Get-ChildItem cmdlet.

In the previous example, I entered the Get-ChildItem cmdlet without any command line switches, which caused PowerShell to display the contents of the current folder. But what if you wanted to take a look at a different folder? In DOS it was possible to append a path to the DIR command (DIR C:\Scripts), and you can do something similar in PowerShell using Get-ChildItem. However, the Get-ChildItem cmdlet does not limit you to specifying a single path. You have the option of examining multiple paths.

Suppose for a moment that I want to examine the contents of the ISO folder and the Scripts folder on my server. I could look at both folders in a single command. The command used for doing so would be:

Get-ChildItem –Path C:\Scripts, C:\ISO

You can see what this looks like below.

Image
PowerShell lets you examine two folders at once.

OK, that’s a fun little party trick, but let’s do something more useful. Let’s pretend that a machine is getting low on storage space, and that you want to get a list of any file that is over 1 GB in size. There are three things that you will need to do in order to accomplish this. First, you will need to enter the command in a way that allows the server’s full contents to be searched (as opposed to manually specifying individual paths). Second, you will have to filter the output based on file size. Third, you will need to put the output into some kind of easily readable format.

The first of these tasks is really easy to accomplish. If you want to examine a server’s full contents, then you can enter the Get-ChildItem cmdlet, followed by the –Recurse switch. The Recurse switch tells PowerShell to display the contents of any folders that exist beneath the current folder. For example, you might enter the command like this:

Get-ChildItem –Recurse

Finding files that are greater than 1 GB in size is also a relatively straightforward process, but you can’t use a Get-ChildItem switch to specify file size. Instead, you will have to pipe the command output into the Where-Object cmdlet, and then use the Where-Object cmdlet to specify the target file size. Such a command might look like this:

Get-ChildItem –Recurse | Where-Object {$_.Length –GT 1GB}

You can see an example of this below.

Image
I was able to list files that were larger than 1 GB.

As you may recall, the last step was to make the output a little bit easier to read. There are a few reasons why we need to do this. First and foremost, you will notice that the Length is listed as a rather large number. We can reformat the length to make it easier to read.

Another reason why we need to clean up the output is because it shows information that we don’t need. For instance, the Mode and LastWriteTime columns are irrelevant to what we are doing.

Finally, we need to suppress error messages. If you look at the image below for example, you can see an error related to a folder to which I do not have rights.

Image
I received an Access Denied error.

To clean up the output, I am going to use this command:

Get-ChildItem –Recurse –ErrorAction SilentlyContinue | Where-Object {$_.Length –GT 1GB} | Select Name, @{Name=”Length”;Expression={$_.Length/1GB}}

You can see the output from this command below.

Image
We have cleaned up the output.

As you can see in the figure above, we have gotten rid of the error message and the unnecessary columns. I used the –ErrorAction switch along with the SilentlyContinue option to get rid of the error. I hid the unnecessary columns by using Select, followed by the names of the columns that I wanted to display. Finally, I converted bytes to gigabytes by using the Expression option and dividing the length by 1 GB. PowerShell also has a way of controlling the number of digits that are displayed, but that’s another task for another time.

Wrapping it up

This article explains how Get-ChildItem is used similarly to the DIR command, and how it can be combined with other PowerShell commands. In the next article, I want to show you some techniques for finding files based on date ranges.

If you would like to read the other parts in this article series please go to:

See Also


The Author — Brien M. Posey

Brien M. Posey avatar

Brien Posey is an MCSE and has won the Microsoft MVP award for the last few years. Brien has written well over 4,000 technical articles and written or contributed material to 27 books.

Advertisement

Featured Links