Home > OS >  Powershell Select-String if contains "xxx" but does not contain "yyy"
Powershell Select-String if contains "xxx" but does not contain "yyy"

Time:03-07

I'm trying to select a string from a debug log which contains a specific string but does not contain a different string.

I guess this is simple but I'm totally new to powershell.

Keep getting same error. "missing statement block after if condition"

Tried a few variations but cant get it to work.

Code i'm trying is: Get-Content -Path "~\filepath\debug.log"-Wait | Select-String "String 1" {if (notcontain (select-string "String 2"))}

Thanks in advance for anyone who can help with this!

CodePudding user response:

You can use -match and -notmatch operators against the content.

Here's something that could do the trick.

$Content = Get-Content -Path "~\filepath\debug.log" -Raw 
$IsMatch = $Content -match 'String 1' -and $Content -notmatch 'String 2'


if ($IsMatch) {
    Write-Host 'Yay' -ForegroundColor Cyan
} else {
    Write-Host ':(' -ForegroundColor Yellow
}

CodePudding user response:

Apply Select-String twice as follows:

Get-Content -Path "~\filepath\debug.log" -Wait |
    Select-String "String 1" |
    Select-String "String 2" -NotMatch
  • Related