.net - 使用 -notcontains 过滤文件

标签 .net windows powershell

我正在尝试过滤等于 .config 的文件扩展名并且文件名中不包含 vshost,因此我运行此脚本:

$files = dir
foreach($file in $files) {
    if($file.Name -notcontains 'vshost' -and $file.Extension -eq '.config') {
        Write-Host $file;
    }
}

但输出仍然包含名称中带有 vshost 的文件:

foo.exe.config
foo.vshost.exe.config
foo2.vshost.exe.config

预期的输出是:

foo.exe.config

我缺少什么以及如何解决这个问题?

最佳答案

-notcontains 用于查找数组中的项目,例如。 $array -notcontains $singleitem。您需要使用 -notlike-notmatch(正则表达式模式)。例如。

if($file.Name -notlike '*vshost*' -and $file.Extension -eq '.config') {

if($file.Name -notmatch 'vshost' -and $file.Extension -eq '.config') {

关于.net - 使用 -notcontains 过滤文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37010783/

相关文章:

c++ - Qt 获取不同操作系统的默认样式表

powershell - 使用Invoke-WmiMethod以特定用户的身份在远程计算机上运行可执行文件

windows - 通过 Jenkins 通过 Powershell 插件安装 appx

c# - 将 DataTable 导出为 CSV 时出现逗号问题

c# - DateTime 转换为 Date,然后在 C# 中返回 DateTime

windows - Cryptoapi 签名/验证不适用于 Windows 8.1

windows - Amazon Workspaces 是否支持 Docker for Windows?

xml - 使用PowerShell在XML文件中进行ForEach

c# - 为什么网络用户通过 NTLM 进行身份验证?

c# - 如何在不调用构造函数的情况下创建对象?