PowerShell for Windows Admins

May 1 2013   2:21PM GMT

Filter vs Include

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I’ve gone through most of the Beginner event submissions over the last couple of days. One thing that has jumped out is the potential misunderstanding around using the –include or –filter parameters on get-childitem.

If we look at the two parameters

-Include
Gets only the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as “*.txt”. Wildcards are permitted.
The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory, such as C:\Windows\*, where the wildcard character specifies the contents of the C:\Windows directory.

-Filter
Specifies a filter in the provider’s format or language. The value of this parameter qualifies the Path parameter.
The syntax of the filter, including the use of wildcards, depends on the provider. Filters are more efficient than other parameters, because the provider applies them when retrieving the objects, rather than having Windows PowerShell filter the objects after they are retrieved.

These are best explained by some examples. Lets start with the current directory

PS> Get-ChildItem -Filter *.csv | select -First 1

Directory: C:\MyData\SkyDrive\Data\scripts

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 12/09/2012 19:07 2665 log.csv

but

Get-ChildItem -include *.csv | select -First 1

returns nothing

To get –Include to work you need to do this

PS> Get-ChildItem -path .\* -include *.csv | select -First 1

Directory: C:\MyData\SkyDrive\Data\scripts

Mode LastWriteTime Length Name
—- ————- —— —-
-a— 12/09/2012 19:07 2665 log.csv

The path has to be given for –Include to return anything

Looking at another folder

These work

Get-ChildItem -Path C:\MyData\SkyDrive\Data\scripts -Filter *.csv

Get-ChildItem -Path C:\MyData\SkyDrive\Data\scripts\* -Filter *.csv

Get-ChildItem -Path C:\MyData\SkyDrive\Data\scripts\* -Include *.csv

Get-ChildItem -Path C:\MyData\SkyDrive\Data\scripts -Include *.csv -Recurse

But these don’t

Get-ChildItem -Path C:\MyData\SkyDrive\Data\scripts -Include *.csv

Get-ChildItem -Path C:\MyData\SkyDrive\Data\scripts\ -Include *.csv

If you want to use –Include remember the path and wildcard or use recurse

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: