Get-ChildItem's -Include Parameter

Learn the trick of using the -Include parameter with Windows PowerShell's Get-ChildItem cmdlet to return just the files you need.

Bill Stewart

December 14, 2009

1 Min Read
ITPro Today logo

Get-ChildItem's -Include parameter serves as a selection pattern for the -Path parameter; only items in the specified path that match the -Include parameter's argument will be returned. However, you must specify a wildcard pattern or filename for the -Path parameter's argument for -Include to work. For example, the following command won't return any results even if you have a C:Data directory that contains .txt files:

get-childitem c:data -include *.txt 

The command doesn't return any results because there's no wildcard pattern for -Include to qualify—it's simply a directory name without a filename pattern. You would use this command instead:

get-childitem c:data* -include *.txt 

This behavior extends to the subdirectories of a directory if you use * with the -include parameter. For example, consider this command:

get-childitem c:data* -include * 

The * wildcard matches directories, too, so this command lists not only the files in C:Data but also the contents of first-level directories under C:Data. To list only the items in C:Data, omit the -Include parameter or specify its argument as $NULL.

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like