4

In powershell is that a way to put multiple search pattern ? Example below command, I want to search those string that has ASA or TMP. But it doesn't work. The search should match as a single word example: ASA is true but TIASAP is false

Get-ChildItem E:\Test\Workflow -Exclude *.bak -Recurse | Select-String -pattern "ASA" -or "TMP" -SimpleMatch | Where-Object LineNumber -le 50 | group path | select name 

2 Answers 2

8

If you omit the -SimpleMatch switch, the Select-String cmdlet uses regex so you can just search for ASA|TMP:

Get-ChildItem E:\Test\Workflow -Exclude *.bak -Recurse | Select-String -pattern "ASA|TMP" | Where-Object LineNumber -le 50 | group path | select name 
Sign up to request clarification or add additional context in comments.

2 Comments

how to match as a single word ? example ASA is ture but YIASAO is false
You can use a word boundary for that \B: So you pattern need to look like this: \bASA\b|\bTMP\b
2

Just pass a list of strings you want to match to the -Pattern parameter.

For example, to search all ps1 files for the words function or check do:

Get-ChildItem *.ps1 | Select-String -Pattern function,check

This would make your example:

Get-ChildItem E:\Test\Workflow -Exclude *.bak -Recurse | Select-String -pattern ASA,TMP -SimpleMatch

Comments

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.