Author Posts

May 7, 2015 at 3:56 am

What is your opinion on this now?

http://m.windowsitpro.com/blog/what-do-not-do-powershell-part-1

Write-Host is the only way to return output to the console in GUI's. Well I shouldn't say only, but quickest.

Dan

May 7, 2015 at 4:04 am

"Console in GUIs?" I'm not sure I know what you mean.

May 7, 2015 at 5:12 am

In writing powershell based forms you have to constantly validate selections, mouseclicks, buttons etc. write-output, write-verbose, write-debug will not output your results to a console.

The only viable options are to write-host, output to file, or write to a text control. Write-host is the most efficient way to return results to the console.

Never say never;-)

May 7, 2015 at 6:29 am

I still say never :). Write-Verbose, for example, works fine for me in that exact scenario, provided I've enabled the pipeline. But I tend to build my own logging function so I can direct output to a file, the console, or whatever, using a flag. That gives me a log for unattended execution as well as live debugging.

May 7, 2015 at 7:38 am

seems pretty arduous a task to do all that when you just want to make sure for example the right value is returned when making a selection from a combobox, no?

May 7, 2015 at 11:08 pm

If you're unhappy with the way Write-Verbose works you can just re-write it. I've built myself quite a module over the last few months that has all kinds of funky functionality. For example, this is my Write-Verbose function:

function Write-Verbose([parameter(mandatory)][string]$Message) {
    if($VerbosePreference -eq 'Continue') {
        Microsoft.PowerShell.Utility\Write-Verbose -Message $Message
        ('{0} VERBOSE: {1}' -f $stopWatch.Elapsed.ToString(), $Message) | Out-File -Append -FilePath $logFilePath -Encoding utf8
    }
}

This captures verbose output and dumps it in a file, as well as writing the message back to the original Write-Verbose command.

Granted with PS5 it should be possible to handle this is an easier way, what with the new output streams and whatnot, but we can't all be putting PS5 into production just yet, even though the guys say WMF5 can handle it.

May 11, 2015 at 7:11 am

FWIW, I'm a big fan of the new Write-Information. I'm less a fan of having Write-Host half hooked up to that. It makes -Host semantically vague. I'd STILL avoid it, and use -Information if that's what I wanted. It's more specific, and there's no risk of a hard to see version incompatibility.

May 13, 2015 at 5:33 am

What version of PS can i find Write-Information or is this a 3rd party add-on?

May 13, 2015 at 6:01 am

oh ok thanks. I have 5.0.9883.0 running on a test machine but i dont see that cmdlet. Coming in a updated version or do i have to enable it?

May 13, 2015 at 10:17 am

I'm not sure if that's in a public build or not yet.