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 <String[]>
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 <String>
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