2

General question for cmdlets: What is the best way to deal with cmdlet log-output. I've read that using Write-Host is not a good idea, because it can NOT be redirected to e.g. a file. Therefore it seems to make sense to use Write-Output for this task. If I also want to return an "object" (to the pipeline) including some results, it will be mixed with the log-output of my cmdlet. Because both, the log-output and the custom result object, are written to the same output stream.

What's a good way to handle this problem? Should I use Write-Verbose if I want to generate some log output?

Moerwald
  • 10,448
  • 9
  • 43
  • 83
  • This should help you http://stackoverflow.com/questions/11161929/show-output-on-screen-and-in-file-in-powershell – Bali C Apr 04 '17 at 10:09
  • 2
    [Related](http://stackoverflow.com/a/38527767/1630171). – Ansgar Wiechers Apr 04 '17 at 10:16
  • It depends on _what_ you want to log and _how_ you want this log to appear. If you want to write to the host tell the user what your cmdlet is doing, `Write-Host` is perfect for you. If you (also) want to log to a file, I'd suggest to `Write-Verbose` and log to a file. I personally don't think one must log cmdlet state to files. – Clijsters Apr 04 '17 at 10:34
  • This could give an overview: [PowerShell Streams](http://stackoverflow.com/documentation/powershell/3255/powershell-streams-debug-verbose-warning-error-output-and-information#t=201704041036476121806) – Clijsters Apr 04 '17 at 10:37

1 Answers1

2

What is the best way to deal with cmdlet log-output

It really depends on the goal of your script / cmdlet or function. In a general sense what you should do is allow for the most options and leave it up to the calling script / cmdlet or function to deal with the output. Write-Verbose would be good if you were supporting the -Verbose switch (and you should be). If your code already needs to use the standard output stream then you need to use at least one different one for other information. Verbose seems like a good fit but it really depends on what the information represents.

I've read that using Write-Host is not a good idea, because it can NOT be redirected to e.g. a file.

That is no longer true with v5. Write-Host got a bad reputation for this but mostly because people misuse it. Ansgar Wiechers' answer on Write-Host vs Write-Information in PowerShell 5 does a good job giving an overview of the different streams. Write-Information would be an obvious choice and you can redirect as you see fit. That was introduced in PowerShell v5 so you could have a portability issue there that needs to be considered.

As long as you have PowerShell v3 or higher then you could redirect verbose output to file, if you wanted to, while leaving the standard output stream alone.

# Location for verbose output
$filename = "c:\temp\verbose.txt"
# Genereate standard, verbose and error output.
$result = $("Bagel";Write-Verbose "Verbose Output"; Write-Error "Error Output") 4> $filename
# Pipeline output will be in $var
$result
# Warning output is in the file
Get-Content $filename

What's a good way to handle this problem?

To repeat: Put as much necessary output, using various streams, in your code and allow the caller to optionally enable it which also allows them to redirect or suppress on a case by case basis.

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119