使用select-string在同一文档中查找2个字符串

CM_*_*man 3 powershell

我正在尝试编写一个powershell命令来搜索包含用户输入的BOTH字符串的文件.现在我可以搜索一个字符串,但无法弄清楚如何确保它有两个字符串.任何帮助将不胜感激.

我在一个存储一堆.SQL文件的本地目录中搜索.文件路径由用户输入(C:\ Program Files\Common Files),然后第一个字符串由用户输入,最后一个字符串.我需要脚本来搜索所有文件,只显示文档中包含两个字符串的文件.

#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
Run Code Online (Sandbox Code Playgroud)

Kei*_*ill 5

一个更简单的方法就是使用Select-String两次:

$filename1 = [regex]::escape($File_Name)
$filename2 = [regex]::escape($File_Name2)
Get-ChildItem -r | Where {!$_.PSIsContainer} | Select-String $filename1 | 
    Select-String $filename2
Run Code Online (Sandbox Code Playgroud)