ro ra
ro ra

Reputation: 419

Measure-Object not outputting the correct result

I am unable to understand why would it result in 0?

[String] $outputVariable = "INFO: Configuring the component HPOvPerlA";
write-host $outputVariable | Select-String -pattern "INFO:" -SimpleMatch | Measure-Object -Line;
                        Lines Words                         Characters                    Property
                        ----- -----                         ----------                    --------
                            0

Where as if i run below then I get correct result.

"INFO: Configuring the component HPOvPerlA" | Select-String -pattern "INFO:" | Measure-Object -Line;
                        Lines Words                         Characters                    Property
                        ----- -----                         ----------                    --------
                            1

The only difference I see is write-host along with $variable but in my opinion this should not make any difference.

Any hint?

Upvotes: 0

Views: 141

Answers (1)

Compo
Compo

Reputation: 38623

The issue you are facing is exactly what you've identified, but seemingly dismissed, and that is Write-Host.

Write-Host sends the $outputVariable/string content to the console, however your intent is to send it through the pipeline. To do that you should be using Write-Output instead.

Examples:

[String] $outputVariable = "INFO: Configuring the component HPOvPerlA";
Write-Output $outputVariable | Select-String -Pattern "INFO:" -SimpleMatch | Measure-Object -Line;
Write-Output "INFO: Configuring the component HPOvPerlA" | Select-String -Pattern "INFO:" | Measure-Object -Line | Select-Object -ExpandProperty Lines;

Upvotes: 1

Related Questions