简体   繁体   English

Windows10 / Powershell:使用Get-Childitem时如何使用-include参数?

[英]Windows10/Powershell: How to use -include parameter when using Get-Childitem?

When using "-filter": 使用“ -filter”时:

Get-ChildItem -file -filter "*.txt" | foreach-object { write-host $_.FullName Get-ChildItem -file -filter "*.txt" | foreach-object { write-host $_.FullName } Get-ChildItem -file -filter "*.txt" | foreach-object { write-host $_.FullName }

I get a listing of the 4 .txt files that's in the current folder. 我得到了当前文件夹中的4个.txt文件的列表。

I tried using "-include" 我尝试使用“ -include”

Get-ChildItem -file -include *.txt | foreach-object { write-host $_.FullName }
Get-ChildItem -file -include *txt | foreach-object { write-host $_.FullName }

and I get nothing. 我什么也没得到。 I tried with and without the "-file" parameter and it makes no difference. 我尝试使用和不使用“ -file”参数,都没有区别。

I've looked at various guides/examples (ss64.com/TechNet and etc) and supposedly I am doing it right. 我看过各种指南/示例(ss64.com/TechNet等),并且据说我做得对。

Any ideas what I could be doing wrong? 有什么想法我可能做错了吗? Thanks! 谢谢!

在此处输入图片说明

From the Get-Help page for Get-ChildItem : Get-ChildItem的“ Get-Help页面:

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. -Include参数仅在命令包含-Recurse参数或路径指向目录内容(例如C:\\Windows* ,其中“ * ”通配符指定C:\\Windows目录。

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. 你会注意到,如果你指定你没有得到一个语法错误-include并没有指定-recurse尽管如此,无论它是从字面上未定义的事实。 You'll also note that C:\\Windows* is not a normal wildcard expression for "all files in the C:\\Windows directory". 您还将注意到,“ C:\\Windows*不是“ C:\\Windows目录中的所有文件”的普通通配符表达式。 It's a wildcard expression for "all items that start with 'Windows' in the C:\\ directory and may or may not have an extension". 它是“ C:\\目录中所有以'Windows'开头且可能具有或不具有扩展名的项目”的通配符表达式。 I have no idea what the authors of Get-ChildItem think this parameter is supposed to do. 我不知道Get-ChildItem的作者认为该参数应该做什么。 They've done a fantastically poor job of documenting it and implementing it. 他们在记录和实施它方面做得非常差。

Consequently, I avoid the -Include parameter as broken/badly documented. 因此,我避免使用-Include参数,因为该文档已损坏/记录不良。 I don't know what it's supposed to do that -Filter doesn't. 我不知道该怎么办-Filter不行。 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. 我的理解是,在每个目录项上调用DirectoryInfo.GetFiles()在每个目录项上调用DirectoryInfo.GetFiles('*.txt')之间是有区别的,但是大多数系统管理员不会知道这意味着什么。 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. 但是,它的行为如此奇怪以至于我不信任它,因此即使我大约95%地确定它会做什么……我仍然从不使用它。

Instead, I just pipe to Where-Object : 相反,我只是管道到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. 还要注意,在某些版本的PowerShell中, Get-ChildItem-LiteralPath-Recurse-Include破坏了,而是返回了所有项目。

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. v6 在这里报告的问题。

These work for me without recursion: 这些对我有用,无需递归:

Get-ChildItem -Path "C:\Users\Athom\Desktop\*.txt"


Get-ChildItem -Path ".\*.txt"

Or Just add the recursion parameter: 或者只需添加递归参数:

 Get-ChildItem -Include *.txt -Recurse

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM