Search word - Powershell

Hello, I need help.

I have text below where and need to get only the word “Completed
Does anyone have a tip?

SessionID: 2020/03 / 31-2
Backup Specification: JOB_BACKUP
Session type: Backup (trans)
Started: Tuesday, March 31, 2020, 12:15:06 AM
Finished: Tuesday, March 31, 2020, 12:15:50 AM
Status: Completed
Number of warnings: 4
Number of errors: 0
User: HI
Host: localhost

It depends on what you want to do with the keyword. For just picking the word, you can use match operator. If you want to validate that the string contains the keyword, you can use Like operator.

$content = get-content filecontainthestring

$content -match “completed”

You will get the keyword inside $Matches variable.

$content -like “completed

You will get output as True.



… and it depends where this text comes from …

… hmmm … sorry, that’s wrong. Have you tried it? When you use a comparison operator like a filter on more than one object you get the resulting object. In this case the line containing the match.

… hhhmmmm … no again … like I mentioned above. You will only get the line containing the match. :wink:

Not tried Olaf, But I have used the same in one of my scripts using foreach, and that worked as expected. Something like the following.

$Content| % {$_ -match "completed"|out-null} 

Name Value
---- -----
0 Completed

Using Foreach-Object is a completely different approach and works as expected then. But you showed something else in your first reply. :wink:

I’d recommend to wait for Maicon to elaborate what he actually want to achieve. :wink:

Agree, based on that experience, I have replied my first post. :slightly_smiling_face:

Good night, sorry for the delay in responding. I forgot to comment, only the word “Completed” appears and nothing else and I have little knowledge in powershell. I need to use Foreach because it has several lines with the same result.

Are you wanting to count how many times completed appears in the content?

No, just show the result.

Based on what you said you need to only capture the word completed. If I understand this correctly, if you have a document with the word completed in it 8 times, you want to end up with a list of 8 instances of the word Completed?


Ok if we assume the line always contains “Status:” before it then the following should work.

Get-Content C:\Temp\Content.txt | foreach{
    if($_ -like 'Status:*'){
        $result = $_.split(':')[1].trim()

If we know it’s always going to be the 6th line, we could do something like this

Get-Content C:\Temp\Content.txt | select -Skip 5 -First 1 | foreach{
    $result = $_.split(':')[1].trim()


In both examples, we are simply picking out the line that has Status: in it, splitting that line at the colon, grabbing the second [1] element, and trimming the extra white space in front of the word Completed. The output is captured in the variable result and is a single string ‘Completed’

I hope this helps!

Hi @Maurer, thanks for your support.
Script one, result appears blank.

Script two, only one result appears

PS C:\dp> C:\dp\Untitled2.ps1

PS C:\dp>

Example Content.txt
SessionID : 2020/03/31-2
Backup Specification: JOByyyyy
Session type : Backup (trans)
Started : terça-feira, 31 de março de 2020, 00:15:06
Finished : terça-feira, 31 de março de 2020, 00:15:50
Status : Completed
Number of warnings : 4
Number of errors : 0
Group : yyyyy
Host : yyyyy

SessionID : 2020/03/31-3
Backup Specification: JOBXXXX
Session type : Backup (trans)
Started : terça-feira, 31 de março de 2020, 01:15:07
Finished : terça-feira, 31 de março de 2020, 01:15:51
Status : Completed
Number of warnings : 4
Number of errors : 0
Group : XXXX
Host : XXXXX

Script one, tested it again and it appeared like script two

Select-String -Path 'C:\Temp\Content.txt' -Pattern 'Completed' -AllMatches | 
    Select-Object -ExpandProperty Matches | 
        Select-Object -Property Value

… or if you just like to get the naked strings:

(Select-String -Path 'C:\Temp\Content.txt' -Pattern 'Completed' -AllMatches).Matches.Value

I see I moved the output of $result outside of the entire foreach loop. I’ve corrected and tested it, however, I much prefer Olaf’s answer. Give them both a try and let us know if it is what you needed.

Thanks Doug Maurer, is work

Get-Content C:\Temp\Content.txt | foreach{
if($_ -like ‘Status:*’){
$result = $_.split(’:’)[1].trim()

Fantastic. You are welcome.