powershell - 在目录中搜索特定的文件类型,如果PowerShell中存在该文件,则返回true或false

标签 powershell filter get-childitem

现在,这可能是一个非常简单的修复程序,但是我似乎无法理解。我正在尝试搜索目录及其特定文件类型的子文件夹。如果文件存在,我的代码当前返回true,但是我也尝试在终端上将其返回false。下面是我的代码

Get-ChildItem "$ScriptPath\$kit" -recurse -include "*.pem"| ForEach-Object {Test-Path $_}

现在,这将在终端上以true的形式返回包含PEM文件的文件夹。但是我想让它在终端上也以false的形式返回给没有文件的文件夹,因为我计划使用truefalse来执行不同的代码集。

最佳答案

$s = 'C:\Users\user\Desktop\Source'

foreach ($d in (Get-ChildItem -Recurse $s -Directory:$true)) {
    if ($d.GetFiles('*.pem')) { 
        $true 
    } else { 
        $false 
    }
}

这应该让您入门...我真的希望PowerShell具有内置的三元构造...

关于powershell - 在目录中搜索特定的文件类型,如果PowerShell中存在该文件,则返回true或false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60834240/

相关文章:

php - 如何在 PHP 中更快地过滤我的电子邮件?

powershell - Powershell,在解析和切换目录名称字符串后重命名目录

filter - Logstash 创建嵌套字段

powershell - 如何使用 jenkins 声明性管道凭证并转换为 powershell 凭证?

powershell - 使用 WinRM for ScriptMethods 增加堆栈大小

powershell - 如何在 PowerShell Core 中 [runtime.interopservices.marshal]::GetActiveObject ("...")?

matlab - 根据 Matlab 中函数的值过滤矩阵行

powershell - PowerShell 脚本中的 Get-ChildItem 权限被拒绝错误

powershell - 根据 "creationdate"复制文件并创建新文件夹

powershell - 为什么在构建服务器上安装 Azure SDK 2.1 或 powershell 3 会破坏我们在 powershell 中运行的一些测试?