删除与数组匹配的行

Jer*_*emy 1 arrays powershell scripting

我有一个完整的行文件,我想删除任何与数组中的字符串匹配的行,使用PowerShell:

$stringsiDoNotWant = @("admin", "student")

$content = gc file.txt
Run Code Online (Sandbox Code Playgroud)

$content = $content -notcontains $StringsiDoNotWant
Run Code Online (Sandbox Code Playgroud)

不起作用:我在变量中仍然有"admin"和"student" $content.

Kei*_*ill 5

试试这个:

(Get-Content file.txt) | Where {$_ -notmatch 'admin|student'} | Out-File somefile.txt
Run Code Online (Sandbox Code Playgroud)

要么

$content = Get-Content file.txt
$content = $content | Where {$_ -notmatch 'admin|student'}
Run Code Online (Sandbox Code Playgroud)