In its basic form the Get-ChildItem cmdlet provides functionality similar to the dir command. For example, if you simply type Get-ChildItem at the Windows PowerShell prompt you’ll get back information about the objects in the current location:
1 2 3 4 5 6 7 8 9 10 11 12 |
Directory: Microsoft.Windows PowerShell.Core\FileSystem::C:\Documents and Settings\user Mode LastWriteTime Length Name ---- ------------- ------ ---- d---- 3/1/2013 9:03 AM Bluetooth Software d---s 5/10/2013 8:55 AM Cookies d---- 5/9/2013 2:09 PM Desktop d-r-- 5/9/2013 8:22 AM Favorites d-r-- 5/9/2013 2:24 PM My Documents d-r-- 3/1/2013 8:15 AM Start Menu d---s 3/1/20133:41 PM UserData d---- 3/16/2013 3:29 PM WINDOWS |
That’s all well and good, but you can do a lot more with Get-ChildItem than simply list the items found in the current location. For example, in the output above you might have noticed that there wasn’t much to look at; that’s because the current location happened to be a folder that contained only a handful of subfolders. Because of that you might have found it a bit more useful if Get-ChildItem had returned not only the names of those subfolders but also the contents of those subfolders; that is, you might want a list of all the files and folders in the subfolders. No problem; just add the -recurse parameter:
1 |
Get-ChildItem -recurse |
Of course, you aren’t limited to working with only the current location; in fact, you aren’t limited to working with just files and folders. Would you like to see a list of all your environment variables? Then simply pass along the path to the environment variable «drive,» like so:
1 |
Get-ChildItem env: |
What about all the registry subkeys found in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall? Why not:
1 |
Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall |
Note. Get-ChildItem cannot be used to retrieve information about the registry values contained within a subkey. For that you need to use the Get-ItemProperty cmdlet.
We could do this all day. For example, the -include and -exclude parameters make it easy to retrieve a specific set of items from a location. Suppose you want information about only the .txt and .log files found in the folder C:\Scripts? That’s easy:
1 |
Get-ChildItem c:\scripts\*.* -include *.txt,*.log |
As you can see, we ask for all the files (*.*) found in the folder C:\Scripts. We then tack on the -include parameter, specifying two file types: *.txt and *.log. (And separating the file types using a comma). What do we get back? We get back only .txt and .log files:
1 2 3 4 5 |
Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 4/6/2013 8:28 PM 3508 950.Log -a--- 4/28/2013 1:16 PM 27 x.txt -a--- 5/8/2013 2:39 PM 25 y.txt |
If we wanted to get back everything except .txt and .log files then we’d simply use the -exclude parameter instead; this parameter tells Windows PowerShell which items should not be included in the returned dataset. Here’s what the command looks like:
1 |
Get-ChildItem c:\scripts\*.* -exclude *.txt,*.log |
Give it a try and see what happens.
The information returned by Get-ChildItem can also be piped into the Sort-Object cmdlet, providing a way to sort the data by in some other format. Would you rather see files sorted by size (length) than by name? Then use this command:
1 |
Get-ChildItem c:\scripts\*.* | Sort-Object length |
Or, if you’d rather see the largest files listed first and the smallest files listed last, then add the -descending parameter:
1 |
Get-ChildItem c:\scripts\*.* | Sort-Object length -descending |