Fun Things You Can Do With the Get- ChildItemCmdlet
It’s another gloomy,
overcast day here in
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- ChildItemC:\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- ChildItemdoes not return information about hidden files or system files.(Which isn’t all that unusual; if you start up Cmd.exe and run the dircommand 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- ChildIteminto showing you allthe 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 thiscommand:
Get- ChildItemC:\Scripts -force
That’s all you have to do: just add the –forceparameter and Get- ChildItemwill 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 onlyin hidden files (equivalent to the command dir /AH)? One way to do that is to run Get- ChildItemwith the –force parameter, then pipe the returned data to the Where-Objectcmdlet:
Get- ChildItemC:\Test -force| Where-Object {$_.mode -match "h"}
As you can see, we use Where-Object to select only those files where the Modeproperty includes the letter h(for “hidden”).
Retrieving Files From Multiple
Folders
If you’ve worked at all with the Get- ChildItemcmdlet then you probably know that, by default, Get- ChildItemshows you only the files in the specified folder (e.g., C:\Scripts); if you’d like to get back information about files in any subfoldersof the specified folder as well then you need to add the – recurseparameter, like so:
Get- ChildItemC:\Scripts - recurse
Nice. But what if you want to retrieve information from three differentfolders, folders that are notsubfolders 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- ChildItemallows 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- ChildItemC:\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 –filterparameter followed by your filter criteria:
Get- ChildItemC:\Scripts, C:\Test, "C:\Documents and Settings\ Kenmyer\Desktop" –filter "*.ps1"
Or, pipe the returned data to the Where-Object cmdlet, like so:
Get-
ChildItemC:\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-Objectcmdlet:
Get- ChildItemC:\Scripts | Select-Object Name
But here’s a faster and easier way; just tack on the –nameparameter when calling Get- ChildItem
Get- ChildItemC:\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- ChildItemcmdlet. Sometimes you don’t really need to know much about the files in a folder; all you really need to know is how manyfiles (if any) can be found in a particular folder. Here’s how you can quickly count the number of files in a folder:
(Get- ChildItemC:\Scripts).Count
What are we doing here? We’re simply using Get- ChildItemto return a collection of all the items found in the folder C:\Scripts; because this isa collection, all we have to do is echo back the value of the Countproperty, which tells us the number of items in the collection. Note the use of parentheses: we enclose the Get- ChildItemcommand in parentheses to ensure that Windows PowerShell first grabs the collection and only thenechoes 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- ChildItemC:\Scripts -filter "*.ps1").Count
And now we aregoing to leave. See you next week with yet another Windows PowerShell tip!
Well, unless the sun is out, that is.