0

I'm trying to create a script that searches through a folders and subfolders searching all the text files for 2 strings. If a file is found then it copys this to another folder. I'm able to do it for 1 string but i can't seem to figure out a way to do an AND in it. (Using -and in the select-string doesnt seem to work)

This is what I have so far(Quite sure the -or doesn't work)

$user = "domain\username"
Invoke-Command -ComputerName Server -ScriptBlock { 
Get-ChildItem -recurse -Include *.txt  -path "File Location" | 
Select-String -pattern ("TAX=1+005+0950" -or "TAX=1+5+0950+0") -SimpleMatch  |  
Copy-Item -Destination "Destination Location"
} -credential $user

I'm needing to search for either of the ones in the code above AND either of these 2 "TAX=1+5+0600" "TAX=1+005+0600"

Any idea how I can do this?

So basically I want to be able to do

Select-String -pattern (TAX=1+005+0950 OR TAX=1+5+0950+0) AND (TAX=1+005+0600 OR TAX=1+5+0600)
8
  • Are you saying that you need to find the existence of one from BOTH lists, or just one from any of the four? That would imply needing an -and (actually a &) but I'm not sure if you don't just need another -or instead (actually a |). Commented May 31, 2016 at 15:11
  • 1
    Possible duplicate of How to use PowerShell select-string to find more than one pattern in a file? Commented May 31, 2016 at 15:16
  • Yeah it needs to find 1 from the first list and 1 from the second. Also it's not a duplicated of the above, the above is for either OR or if ones comes after the other. Commented May 31, 2016 at 15:16
  • Actually, if you read the answers, you'll see that they fit your needs exactly. They do two consecutive, piped, select-string commands, one from the first list, and one from the second. Commented May 31, 2016 at 15:20
  • Yes but then how do I do an -OR statement? E.G where { $_ | Select-String -Pattern 'TAX=1+005+0950' -or 'TAX=1+5+0950' } | where { $_ | Select-String -Pattern 'TAX=1+005+0600' -or 'TAX=1+5+0600' } | This doesn't work Commented May 31, 2016 at 15:29

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.