How to get only STDOUT: value from command output in powershell

In my powershell script, I have bolt command as below

bolt command run hostname --targets winrm://158.28.0.546 --no-ssl -user testuser123 -password test@84p

Above command executes successfully and returns output as below

Starting on…
Finished at…
STDOUT:
VALUE123
Success at…
Run on …

From above output, I want to eliminate messages like Starting on,Finished at, Success at , Run on … etc.

I want to return only STDOUT: values from command.

I want to have command similar to below, for getting only STDOUT: value

bolt command run hostname --targets winrm://158.28.0.546 --no-ssl -user testuser123 -password test@84p | Select STDOUT:

How to get only only STDOUT: value from commad output.

Kindly suggest.

bolt is not a powershell command. The PowerShell interpreter is just calling the associated executable using your syntax. You could just run that command in a cmd.exe shell. The results of the command are returned as a string object to PS. Recommend reviewing documentation on that command.

Assuming Bolt running on a windows machine (not Linux), and that the text output is a single [String] not a string array [String],
You’ll just have to parse it manually like you do in Bash as in:

$SampleBotOutput = @'
Starting on..
Finished at..
STDOUT:
VALUE123
other lines
bla bla bla
Success at..
Run on ..
'@

$StartMarker = 'STDOUT:'
$EndMarker   = 'Success at'

$myStdOut = ((($SampleBotOutput -split $StartMarker)[1] -split $EndMarker)[0]).Trim()

"STDOUT = ($myStdOut)"

All of the output is from STDOUT. If you assign a variable you may need to add the call operator. I would test success and fail scenarios to see if the parse would work, but if it’s always the same line you can do something simple like this:

$output = @"
Starting on..
Finished at..
STDOUT:
VALUE123
Success at..
Run on ..
"@

#Always the same number of lines
#($output -split [environment]::NewLine)[3]

#Select String to find the line with STDOUT and get next line
($output -split [environment]::NewLine) | Select-String 'STDOUT:' -Context 0,1 | ForEach-Object {
    $_.Context.PostContext
}

Check out Adam’s post where he offers a small script to take care of this for you. I think it will help you achieve your goal.

https://adamtheautomator.com/powershell-start-process/

I hope this is helpful!