Powershell 使用Get-ChildItem-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

我的C:目录中有一个包含许多文件的列表。如果我尝试对其运行-Exclude,则不会得到任何返回。与-Include相同。如果我使用过滤器,它将返回我期望返回的内容。我不明白它应该做什么吗

下面是一个我正在跑步却一无所获的例子:

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:\
      ,我假设这就是您在本答案的其余部分中的意思
  • 使用
    -Exclude
    (以及
    -Include
    )参数,既不使用
    -Recurse
    ,也不使用以
    \*
    结尾的
    -Path
    值,通常不会产生任何结果。
    意外?事实上,更多信息请参见下文

    • 因此,
      Get Item-Path C:\*-Exclude*.txt
      -注意从
      Get ChildItem
      Get Item
      的切换,并且需要
      C:\
      之后的
      *
      -才能使您的命令仅用于直接位于
      C:\
      中的项

背景资料: 使用提供者本机
-Filter
参数通常优于
-Include
,因为:

  • 它比
    -Include
    快得多,因为提供程序本身在源代码处执行过滤,而不是让PowerShell在接收到所有对象之后应用过滤器

  • 它不需要您切换到
    Get Item
    并将
    \*
    附加到
    -Path
    参数值

    • 例如,
      getchilditem-Path C:\-Filter*.txt
      可以很好地匹配C:根目录中的所有
      *.txt
      文件
也就是说,这里有一些警告:

  • -Filter
    支持的通配符模式语言比PowerShell的功能更少—值得注意的是,它不支持字符集/范围,例如
    [0-9]
    —可能会意外地匹配短(8.3)文件名,并且有其他遗留问题—有关详细信息,请参阅

  • -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
    /
    -Exclude
    被悄悄忽略。从(至少)v7.0开始,PowerShell(Core)中已经修复了此问题(可以公平地假设,它不会在Windows PowerShell中修复,Windows PowerShell只会继续接收关键修复)

  • -包括
    -排除
    不按直觉预期工作
    ,这是以下主题:

    • -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