我正在所有的文件夹中查找一个文件。

Copyforbuild.bat在很多地方都有,我想递归搜索。

$File = "V:\Myfolder\**\*.CopyForbuild.bat"

如何在PowerShell中做到这一点?


当前回答

试试这个:

Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse | Where-Object { $_.Attributes -ne "Directory"}

其他回答

Get-ChildItem V:\MyFolder -name -recurse *.CopyForbuild.bat

也可以

当搜索文件夹时,你可能会得到一个基于安全性的错误(例如C:\Users),使用以下命令:

Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse -ErrorAction SilentlyContinue -Force

使用通配符过滤:

Get-ChildItem -Filter CopyForBuild* -Include *.bat,*.cmd -Exclude *.old.cmd,*.old.bat -Recurse

使用正则表达式进行过滤:

Get-ChildItem -Path "V:\Myfolder" -Recurse
| Where-Object { $_.Name -match '\ACopyForBuild\.[(bat)|(cmd)]\Z' }

试试这个:

Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse | Where-Object { $_.Attributes -ne "Directory"}

使用带有-递归开关的Get-ChildItem cmdlet:

Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse -ErrorAction SilentlyContinue -Force