Powershell 使用Get-ChildItem-Exclude或-Include不会返回任何结果
我的C:目录中有一个包含许多文件的列表。如果我尝试对其运行-Exclude,则不会得到任何返回。与-Include相同。如果我使用过滤器,它将返回我期望返回的内容。我不明白它应该做什么吗 下面是一个我正在跑步却一无所获的例子:Powershell 使用Get-ChildItem-Exclude或-Include不会返回任何结果,powershell,powershell-4.0,Powershell,Powershell 4.0,我的C:目录中有一个包含许多文件的列表。如果我尝试对其运行-Exclude,则不会得到任何返回。与-Include相同。如果我使用过滤器,它将返回我期望返回的内容。我不明白它应该做什么吗 下面是一个我正在跑步却一无所获的例子: Get-ChildItem -Path C: -Exclude "*.txt" 我没有得到任何回报。如果我跑 Get-Childitem -filter "*.txt" 我把这个拿回来: Mode LastWriteTime
Get-ChildItem -Path C: -Exclude "*.txt"
我没有得到任何回报。如果我跑
Get-Childitem -filter "*.txt"
我把这个拿回来:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 11/7/2007 8:00 AM 17734 eula.1028.txt
-a---- 11/7/2007 8:00 AM 17734 eula.1031.txt
-a---- 11/7/2007 8:00 AM 10134 eula.1033.txt
-a---- 11/7/2007 8:00 AM 17734 eula.1036.txt
-a---- 11/7/2007 8:00 AM 17734 eula.1040.txt
-a---- 11/7/2007 8:00 AM 118 eula.1041.txt
-a---- 11/7/2007 8:00 AM 17734 eula.1042.txt
-a---- 11/7/2007 8:00 AM 17734 eula.2052.txt
-a---- 11/7/2007 8:00 AM 17734 eula.3082.txt
7/7/2016 8:50 AM 93 HaxLogs.txt
-a---- 7/8/2016 8:30 AM 0 Test.txt
本例介绍了-Include
的工作原理,将为您提供预期的结果。请注意,我在path参数中也提供了一个通配符,以将路径显式定义为“根C中的任何文件:”而不是“C:”本身
资料来源:
示例3,如果该链接失效(请注意此处路径中的通配符):
使用“C:”后不加斜杠是无法获得所需结果的原因。这被解释为当前目录的相对路径,而不是C驱动器的根目录。(见:) 如果改用“C:\”,它应该可以正常工作
编辑:我的错误;我是专门针对那些只使用“-exclude.”来总结和补充有用答案的示例做出回应的: 您的方法有两个明显的问题:
- 定位
可能(并不总是)做你想做的事,因为C:
指的是此时驱动器C:上的当前位置(工作目录)。C:
- 要以驱动器C:的根文件夹为目标,您必须使用
,我假设这就是您在本答案的其余部分中的意思C:\
- 要以驱动器C:的根文件夹为目标,您必须使用
- 使用
(以及-Exclude
)参数,既不使用-Include
,也不使用以-Recurse
结尾的\*
值,通常不会产生任何结果。意外?事实上,更多信息请参见下文-Path
- 因此,
-注意从Get Item-Path C:\*-Exclude*.txt
到Get ChildItem
的切换,并且需要Get Item
之后的C:\
-才能使您的命令仅用于直接位于*
中的项C:\
- 因此,
背景资料: 使用提供者本机
-Filter
参数通常优于-Include
,因为:
- 它比
快得多,因为提供程序本身在源代码处执行过滤,而不是让PowerShell在接收到所有对象之后应用过滤器-Include
- 它不需要您切换到
并将Get Item
附加到\*
参数值-Path
- 例如,
可以很好地匹配C:根目录中的所有getchilditem-Path C:\-Filter*.txt
文件*.txt
- 例如,
支持的通配符模式语言比PowerShell的功能更少—值得注意的是,它不支持字符集/范围,例如-Filter
—可能会意外地匹配短(8.3)文件名,并且有其他遗留问题—有关详细信息,请参阅[0-9]
仅支持单个模式,而-Filter
支持多个模式(一组模式)-Include
-Filter
始终是一个正向(inclusionary)过滤器,因此不能用于提供-Exclude
的功能
使用
Get ChildItem
实现-Include
/-Exclude
是不直观的,并且存在缺陷:
旁注:如果只使用一个-Include
模式(与-Exclude
相反),则更容易将模式直接附加到-Path
参数;e、 g.:Get ChildItem C:\*.txt
tl;博士:
要获得可预测的行为,请使用-Include
/-Exclude
,如果您未同时使用-Recurse
(如果您使用-Recurse
,则不需要此解决方法):
#重要提示:如果使用-Recurse,则不需要解决方法。
#*“\*”已附加到输入路径
#*获取-*子项*已切换为获取-*项*
获取项目C:\path\到\*-包括。。。
获取项目C:\path\到\*-排除。。。
在PowerShell(Core)v7+中,如果您的输入路径是文本路径,您也可以将Get ChildItem
与-LiteralPath
一起使用,而不是(可能是位置隐含的)-Path
(对于当前目录,使用
):
#重要提示:仅在PowerShell(核心)中工作。
#请注意-LiteralPath的使用。
获取ChildItem-LiteralPath C:\path\to-Include。。。
获取ChildItem-Literalpath C:\path\to-Exclude。。。
- Windows PowerShell中有一个明显的bug,当使用
时,-LiteralPath
/-Include
被悄悄忽略。从(至少)v7.0开始,PowerShell(Core)中已经修复了此问题(可以公平地假设,它不会在Windows PowerShell中修复,Windows PowerShell只会继续接收关键修复)-Exclude
和-包括
不按直觉预期工作,这是以下主题:-排除
和-Include
修改-Exclude
参数的叶(最后一个)路径组件,即文件系统路径中的文件或目录名-path
- 这意味着模式首先应用于指定文件夹路径本身的叶组件,然后再应用于子项(如果有的话)
- 如果输入路径不是以
Get-ChildItem -Path "C:\*" -Include "*.txt"
C:\> Get-ChildItem –Path "C:\Windows\Logs\*" -Include "*.txt" -Exclude "A*"
# HAPPENS TO WORK, BUT IS NOT ROBUST: # Matches all w* items *inside* C:\Windows, but # ONLY because w* happens to match 'Windows' - the last input # path component - too. Get-ChildItem C:\Windows -Include w* # HAPPENS TO WORK, BUT IS NOT ROBUST: # Matches all items whose names *don't* start with a-v *inside* C:\Windows, but # ONLY because [a-v]* happens not to exclude 'Windows' - the last input # path component - too. Get-ChildItem C:\Windows -Exclude [a-v]* # OUTPUTS NOTHING: # Because t* doesn't match 'Windows', the child items of # 'C:\Windows' are not considered. Get-ChildItem C:\Windows -Include t* # OUTPUTS NOTHING: # Because w* matches 'Windows', it is excluded, and # the child items of 'C:\Windows' are not considered. Get-ChildItem C:\Windows -Exclude w*
get-childitem c:\windows -directory -exclude *.dll
get-childitem Microsoft.PowerShell.Core\FileSystem::C:\ -exclude *.txt get-childitem : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again. At line:1 char:1 + get-childitem Microsoft.PowerShell.Core\FileSystem::C:\ -exclude wind ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-ChildItem], PSArgumentException + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.GetChildItemCommand
get-childitem / -directory -exclude *.txt get-childitem : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again. At line:1 char:1 + get-childitem / -directory -exclude var + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-ChildItem], PSArgumentException + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.GetChildItemCommand
get-item c:\ | get-childitem -exclude *.txt