0

How do I get the Get-Content command to exit if a string match is found?

e.g. When "string to find" is matched in the followed file, how to exit the Get-Content command?

Get-Content file -wait | ? { $_ -match "string to find" }
Trevor H
  • 35
  • 3

1 Answers1

0

Break your problem into steps you can easily read and debug.

Perhaps something like this is a starting example:

One line:

$files = gci *.txt; foreach ($f in $files) {$s='XXX';"checking '$f'";if (select-string -Path $f -Pattern $s) { "file '$($f.Name)' contains '$s'"; break;}}

Expanded for readability, debug-ability, etc.:

$files = gci *.txt
foreach ($f in $files) 
{
    $s='XXX'
    "checking '$f'"
    if (select-string -Path $f -Pattern $s)
    {
        "file '$($f.Name)' contains '$s'"
        break
    }
}
Kory Gill
  • 6,993
  • 1
  • 25
  • 33