TheGameiswar
TheGameiswar

Reputation: 28890

Powershell match exact string from list of lines

I get below output from a query in powershell.I need to check if the output contains success

Output:

$a = '
Executing command: this command

url : https://www.google.com

Working on it...

unSucessfull!' 

one more output, that could be returned from this

$a = '
    Executing command: this command

    url : https://www.google.com

    Working on it...

    Success!' 

I need to check, If this string contains exact word "Success"

So i did below

$a |select-string "Success" -simplematch

But this matches unsuccessfull word as well

I tried many variations,but nothing helped like with out any success

$a -like "Success*"

Any pointers

Upvotes: 2

Views: 20471

Answers (2)

Nirav Mistry
Nirav Mistry

Reputation: 989

You can also use Regex in Select-String cmdlet to fulfill your requirements

Here is the example : (Note : as per given text there are spaces before 'Success' )

$result = $a | select-string -Pattern "`r`n[\s]*Success"

if string does not exist in input then $result will be null.

Upvotes: 0

Lee_Dailey
Lee_Dailey

Reputation: 7479

if all you want is "is that exact word in the text?" then you should use -match instead of Select-String. [grin] the 1st gives you a false/true result, but the 2nd is for finding & returning a string that contains the text.

so the proper solution to "is that exact word present in the string?" is ...

$A -match '\bsuccess\b'

that checks for the search term bounded by word boundaries and gives you either False or True.

Upvotes: 8

Related Questions