다음을 통해 공유


Using the Get-ChildItem Cmdlet

Replicating (and Extending) the DIR Command

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:

Directory: Microsoft.Windows PowerShell.Core\FileSystem::C:\Documents and Settings\kenmyer

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----          3/1/2006   9:03 AM            Bluetooth Software
d---s         5/10/2006   8:55 AM            Cookies
d----          5/9/2006   2:09 PM            Desktop
d-r--          5/9/2006   8:22 AM            Favorites
d-r--          5/9/2006   2:24 PM            My Documents
d-r--          3/1/2006   8:15 AM            Start Menu
d---s          3/1/2006   3:41 PM            UserData
d----         3/16/2006   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:

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:

Get-ChildItem env:

What about all the registry subkeys found in HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall? Why not:

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:

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:

Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          4/6/2006   8:28 PM       3508 950.Log
-a---          5/6/2006  10:06 AM          0 Employees.txt
-a---          5/6/2006  10:06 AM          0 Employees_NODUPL.txt
-a---          5/6/2006  10:06 AM          0 ExcelData.txt
-a---          3/3/2006   9:24 PM      14894 methods.txt
-a---         4/28/2006   1:36 PM         41 new_name.txt
-a---          3/7/2006   1:44 PM       4112 read-write.txt
-a---         4/11/2006  11:04 AM         18 servers.txt
-a---          5/5/2006   9:09 PM      53358 tee.txt
-a---         4/26/2006  12:28 PM       1125 temporary_print_file.txt
-a---          5/6/2006  10:30 PM      34184 test.log
-a---          5/9/2006   3:17 PM         58 test.txt
-a---          4/6/2006  10:26 PM        205 test_NODUPL.txt
-a---         4/28/2006   1:16 PM         27 x.txt
-a---          5/8/2006   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:

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:

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:

Get-ChildItem c:\scripts\*.* | Sort-Object length -descending
Get-ChildItem Aliases
  • gci

  • ls

  • dir