Get-ChildItem不适用于Include

时间:2017-01-17 15:11:55

标签: powershell

enter image description here

示例我正在关注:

Get-ChildItem c:\scripts\*.* -include *.txt,*.log

https://technet.microsoft.com/en-us/library/ee176841.aspx

是什么给出的?当我尝试使用include?时,为什么不取回我的test.txt文件列表?

作为旁注,c:\scripts\*.*是什么。它似乎在说包含任何具有任何格式名称的文件。但是不包含在指定中吗?无论如何,更感兴趣的是为什么我看似基本的代码不起作用。

2 个答案:

答案 0 :(得分:13)

来自the help file (Get-Help Get-ChildItem)

  

仅当命令包含 Recurse 参数或路径指向目录的路径时,Include参数才有效,例如C:\ Windows *,通配符指定C:\ Windows目录的内容。

Get-ChildItem c:\pstest\*.* -include *.txt

Get-ChildItem c:\pstest -recurse -include *.txt

或更好:使用-Filter参数代替-Include

Get-ChildItem C:\pstest -Filter *.txt

答案 1 :(得分:1)

你也可以这样做:

Get-ChildItem "C:\pstest\*.txt"

如果您想创建更精细的过滤器:

Get-ChildItem "C:\pstest\" -recurse -file | where {$_.Name -like "*hello*" -and $_.Name -notlike "*good by*"}