带有通配符的Get-ChildItem路径中没有“ Where”

时间:2019-06-26 06:47:53

标签: windows powershell ps

我想在传递给Get-ChildItem的路径中使用通配符。因此,在示例中,我想使用C:\root\*\container作为路径参数。但这仅在以下1级中搜索。如果我写C:\root\*\*\container,那么它将只搜索下面的2个级别。

我尝试做类似Get-ChildItem -Path "C:\root\*\container\vc$number" "*test*.exe" -Recurse的事情 并将结果复制到特定目录。如果我在C:\root中进行递归搜索,则会发现文件过多。如果我使用示例中给出的路径,那么我仅搜索以下一级,而不是在所有目录中递归搜索(甚至可以搜索5级)。我知道我可以使用

Get-ChildItem -Path "C:\root\" "*test*.exe" -Recurse | Where { %_.FullName -like "container\vc$number" }

但是我想知道是否可以跳过使用Where并在路径中使用通配符。原因是我从外部文件读取路径,有些路径包含通配符(如上面的示例),有些则不包含。因此,我希望不必编写处理路径并使用Get-ChildItem有/无Where

的函数

所以在示例中我有

C:\root\container\*test*.exeC:\root\a\container\*test*.exeC:\root\b\container\*test*.exeC:\root\c\x\y\container\*test*.exeC:\root\c\x\y\z\g\container\*test*.exe

,依此类推。我想用C:\root\*\container找到所有的人

1 个答案:

答案 0 :(得分:1)

Get-Childitem具有参数Filter,可用于过滤所需的结果。在您的情况下(据我了解),您想在名为“容器”的所有目录中获取文件。

首先,您必须获取这些目录的路径,然后按如下所示获取内部文件:

Get-ChildItem "C:\root" -filter "*container*" -recurse | Get-ChildItem

输出

C:\root\a\container\new.exe
C:\root\b\container\sample.exe
C:\root\c\x\y\z\g\container\anything.exe

我最后使用了.Fullname使其显示为上方