MaheshMohan
MaheshMohan

Reputation: 59

Search for Multiple strings in a text file in Powershell

I have a text 'File.txt'. There are 100's of lines. The file contains the string 'XX' (in any line), 'YY' (in any line) and 'ZZ' (in any line).
I want to check if the text file really contains 'XX' or 'YY' or 'ZZ'. If it yes then exit the script.
I'm not sure how to give multiple search patterns in the below line Or any modification to this existing code would help.

$myString = Select-String -Path C:\Temp\File.txt -Pattern "XX"

Edited Code:

$myFile = Get-Content -Path 'C:\file.txt | Out-String    
if (Select-String $myFile -Pattern 'XX|YY' -NotMatch)    
{    
Do something else    
} 

Upvotes: 1

Views: 10672

Answers (1)

arco444
arco444

Reputation: 22881

Select-String accepts a regular expression as a pattern, so you can just use a logical OR to check for all three strings:

if ( Select-String -Path C:\Temp\File.txt -Pattern 'XX|YY|ZZ' ) {
  echo "yes"
  # Do something else
}

Upvotes: 1

Related Questions