get-childitem 中 -include 和 -filter 的区别

Difference between -include and -filter in get-childitem

谁能解释一下 Get-ChildItem 命令中 -Include-Filter 选项的区别。

下面是我要执行的两段代码。它们都用于查找特定目录中的文本文件:

PS C:\Users2997> get-childitem -path Desktop\Extras -filter *.txt


    Directory: C:\Users2997\Desktop\Extras


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        12/22/2014   4:05 PM        140 Expense_report.txt
-a---         1/14/2015   4:41 PM        211 Extras.txt
-a---         2/10/2015   2:46 PM        259 Learn Dutch.txt

PS C:\Users2997> get-childitem -path Desktop\Extras -include *.txt

--以上命令没有产生结果----

  1. Filter 参数由提供商实现。它很有效,因为在检索对象时适用。 Get-PSprovider commandlet 显示实现 'filter' 参数的提供程序。例如,我的上只有两个提供者 system:ActiveDirectory 和文件系统

  2. Include参数由Powershell实现。它只能与 Recurse 参数结合使用(如 MSDN 所述 here)。

  3. 有趣的是:

    get-childitem -path Desktop\Extras\ -include *.txt
    

    returns 没什么

    get-childitem -path Desktop\Extras\* -include *.txt
    

    returns *.txt 文件列表

也许这些只是实施的细微差别。

另请参阅此优秀博客 post:http://tfl09.blogspot.com/2012/02/get-childitem-and-theinclude-and-filter.html

-filter 应该比 -include 快。 -filter 可以匹配 powershell 5.1 中文件名的短版本。