Windows PowerShell Tip of the Week

Here’s a quick tip on working with Windows PowerShell. These are published every week for as long as we can come up with new tips. If you have a tip you’d like us to share or a question about how to do something, let us know.

Fun Things You Can Do With the Get-ChildItem Cmdlet

It’s another gloomy, overcast day here in Seattle, and there’s nothing to do, nothing at all. But you know the old saying, “When things are at their darkest, there’s only one thing you can do to brighten your day: get out your copy of Windows PowerShell and take a look at some of the cool things you can do with the Get-ChildItem cmdlet.” And who are we to argue with a wise old saying like that?

Retrieving Hidden Files and System Files

Let’s start off by discussing a situation you’ve likely run into, maybe without even realizing it. Suppose we have the following set of files in the folder C:\Scripts:

File1.txt
File2.txt
File3.txt
File4.txt
File5.txt

Let’s further suppose that the files File3.txt and File4.txt are both hidden files. What do you suppose we’ll get back when we run the command Get-ChildItem C:\Scripts? You’re absolutely right; this is what we’ll get back:

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         7/26/2007   9:13 AM          0 File1.txt
-a---         7/26/2007   9:13 AM          0 File2.txt
-a---         7/26/2007   9:13 AM          0 File5.txt

Why only three files? Because, by default, Get-ChildItem does not return information about hidden files or system files. (Which isn’t all that unusual; if you start up Cmd.exe and run the dir command you’ll get the same results.) That’s a bit of a bummer, isn’t it?

No, not really; fortunately, there’s a very simple way to entice Get-ChildItem into showing you all the files in a folder, including system and hidden files. In Cmd.exe you can get a list of all files in a folder (including hidden files and system files) by running the command dir /a; in Windows PowerShell, you can get a list of all the files in a folder (including hidden files and system files) by running this command:

Get-ChildItem C:\Scripts -force

That’s all you have to do: just add the –force parameter and Get-ChildItem will return a list of all the files in a folder, like so:

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---         7/26/2007   9:13 AM          0 File1.txt
-a---         7/26/2007   9:13 AM          0 File2.txt
-a-h-         7/26/2007   9:13 AM          0 File3.txt
-a-h-         7/26/2007   9:13 AM          0 File4.txt
-a---         7/26/2007   9:13 AM          0 File5.txt

What if you’re interested only in hidden files (equivalent to the command dir /AH)? One way to do that is to run Get-ChildItem with the –force parameter, then pipe the returned data to the Where-Object cmdlet:

Get-ChildItem C:\Test -force| Where-Object {$_.mode -match "h"}

As you can see, we use Where-Object to select only those files where the Mode property includes the letter h (for “hidden”).

Retrieving Files From Multiple Folders

If you’ve worked at all with the Get-ChildItem cmdlet then you probably know that, by default, Get-ChildItem shows you only the files in the specified folder (e.g., C:\Scripts); if you’d like to get back information about files in any subfolders of the specified folder as well then you need to add the –recurse parameter, like so:

Get-ChildItem C:\Scripts -recurse

Nice. But what if you want to retrieve information from three different folders, folders that are not subfolders of one another? (For example, C:\Scripts, C:\Test, and C:\Documents and Settings\Kenmyer\Desktop.) Well, here’s one suggestion: just go ahead and retrieve information from all three of those folders. As it turns out, Get-ChildItem allows you to specify multiple folder paths; all you have to do is separate the paths with commas (and, if the path includes a blank space, enclose the path name in double quote marks):

Get-ChildItem C:\Scripts, C:\Test, "C:\Documents and Settings\Kenmyer\Desktop"

Interested only in a subset of the files found in those three folders (for example, only in the .PS1 files)? No problem; just add the –filter parameter followed by your filter criteria:

Get-ChildItem C:\Scripts, C:\Test, "C:\Documents and Settings\Kenmyer\Desktop" -filter "*.ps1"

Or, pipe the returned data to the Where-Object cmdlet, like so:

Get-ChildItem C:\Scripts, C:\Test, "C:\Documents and Settings\Kenmyer\Desktop" | 
Where-Object {$_.Extension -eq ".ps1"}

Didn’t we tell you this would be fun?

Retrieving Only File and Folder Names

Here’s a trick that might come in handy from time-to-time. When working with files and folders you’ll often want just the file name. One way to do that is to retrieve all the information and then pipe the data to the Select-Object cmdlet:

Get-ChildItem C:\Scripts | Select-Object Name

But here’s a faster and easier way; just tack on the -name parameter when calling Get-ChildItem

Get-ChildItem C:\Scripts -name

Give that a try and see what happens.

Counting the Number of Items in a Folder

Well, what do you know: it looks like the sun is finally coming out, which means it’s almost time for us to go. Before we do, however, let’s show you one last little trick with the Get-ChildItem cmdlet. Sometimes you don’t really need to know much about the files in a folder; all you really need to know is how many files (if any) can be found in a particular folder. Here’s how you can quickly count the number of files in a folder:

(Get-ChildItem C:\Scripts).Count

What are we doing here? We’re simply using Get-ChildItem to return a collection of all the items found in the folder C:\Scripts; because this is a collection, all we have to do is echo back the value of the Count property, which tells us the number of items in the collection. Note the use of parentheses: we enclose the Get-ChildItem command in parentheses to ensure that Windows PowerShell first grabs the collection and only then echoes back the value of the Count property for that collection.

And sure, you can include a filter when calling Get-ChildItem. Need to know how many .PS1 files are in the folder C:\Scripts? Okey-doke:

(Get-ChildItem C:\Scripts -filter "*.ps1").Count

And now we are going to leave. See you next week with yet another Windows PowerShell tip!

Well, unless the sun is out, that is.