What is the right way to capture the output of Format-Table (to show it on the host, rather than the stdout)?

Viewed 37

I’ve got a few functions that return an array of PsCustomObjects, and returning other stuff would be bad.

Yet, at times, I’d like to pass in -Verbose and spit out some information to the host (using Write-Host). I can easily implement Test-Verbose, but when I want to spit out a table (with Format-Table), it’s not as easy as capturing the output and piping it to Write-Host.

What is the right way to show the output of Format-Table to the host, without sending it to the stdout, where it will be tallied as part of the function’s return values?

For example:

$objects | Format-Table | Write-Host

Thanks!!

1 Answers

You should be able to do this with Out-String. For example, change you example to:

$objects | Format-Table | Out-String | Write-Host

Does that work?

Related