Using select-string to find 2 strings in the same document

Viewed 3910

I am trying to write a powershell command to search files that contain BOTH strings input by the user. Right now I can search for the one string, but can not figure out how to make sure that it has both strings. Any help would be greatly appreciated.

I am searching in a local directory that stores a bunch of .SQL files. The file path is input by the user (C:\Program Files\Common Files), Then the first string is input by the user, and last the second string. I need the script to search thru all the files and only display the files with both strings in the document.

#Requests the loctation of the files to search
$Location = Read-Host 'What is the folder location of the files you want to search?'

#Sets the location based off of the above variable
Set-Location $Location 

#Sets the alias
Set-Alias ss Select-String 

#Requests the text to search for in the files
$File_Name = Read-Host 'Object Name?'
$File_Name2 = Read-Host 'Second Object Name'

#Searches the files and returns the filenames
Get-ChildItem -r | Where {!$_.PSIsContainer} | ss -Pattern '($File_Name|$File_Name2)'  | Format-List FileName
3 Answers
Related