From the Get-Help
page for Get-ChildItem
:
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.
You'll note that you don't get a syntax error if you specify -include
and don't specify -recurse
in spite of the fact that whatever it does is literally undefined. You'll also note that C:\Windows*
is not a normal wildcard expression for "all files in the C:\Windows
directory". It's a wildcard expression for "all items that start with 'Windows' in the C:\
directory and may or may not have an extension". I have no idea what the authors of Get-ChildItem
think this parameter is supposed to do. They've done a fantastically poor job of documenting it and implementing it.
Consequently, I avoid the -Include
parameter as broken/badly documented. I don't know what it's supposed to do that -Filter
doesn't. I've read articles about what it does exactly. It "passes the value to the underlying provider to filter at that level" in some manner. I don't know why they assume that a sysadmin will know what that really means. My understanding is that it's the difference between calling DirectoryInfo.GetFiles()
on each directory item and calling DirectoryInfo.GetFiles('*.txt')
on each directory item, but most sysadmins aren't going to know what that means. However, it's so oddly behaved that I don't trust it, so even though I am about 95% sure of what it does... I still never use it.
Instead, I just pipe to Where-Object
:
Get-ChildItem -file | Where-Object Extension -eq '.txt' | [...]
Also note that Get-ChildItem
is broken with -LiteralPath
, -Recurse
and -Include
in some versions of PowerShell, and will instead return all items.
Compare:
Get-ChildItem -LiteralPath $PSHOME *.exe -Recurse # works
Get-ChildItem -Path $PSHOME -Include *.exe -Recurse # works
Get-ChildItem -LiteralPath $PSHOME -Include *.exe -Recurse # does NOT work
Issue reported here for v6.