PowerShell Great Debate: "Fixing" Output

When should a script (or more likely, function) output raw data, and when should it "massage" its output?

The classic example is something like disk space. You're querying WMI, and it's giving you disk space in bytes. Nobody cares about bytes. Should your function output bytes anyway, or output megabytes or gigabytes?

If you output raw data, how would you expect a user to get a more-useful version? Would you expect someone running your command to use Select-Object on their own to do the math, or would you perhaps provide a default formatting view (a la what Get-Process does) that manages the math?

The "Microsoft Way" is to use a default view - again, it's what Get-Process does. But views are separate files, and they're only really practical (many say) when they're part of a module that can auto-load them.

What do you think?

[boilerplate greatdebate]

About Don Jones

Don Jones is a Windows PowerShell MVP, author of several Windows PowerShell books (and other IT books), Co-founder and President/CEO of PowerShell.org, PowerShell columnist for Microsoft TechNet Magazine, PowerShell educator, and designer/author of several Windows PowerShell courses (including Microsoft's). Power to the shell!

4 thoughts on “PowerShell Great Debate: "Fixing" Output

  1. Dave Wyatt

    For the most part, my functions (whether in modules or not) return raw data, but there's also a layer of script code that's responsible for console input / output, and making calls to those functions. That's where I put calls to Read-Host, Write-Host, Format-Table, etc, and where I change things like bytes to gigabytes.

    I haven't messed with views or .format.ps1xml files in my modules yet, though I might at some point.

  2. Mike Shepard

    I agree with Dave. In functions that return objects, I don't tend to "fix" data. In functions that produce output for reporting, I might format things like disk volumes, for example.

  3. June Blender

    I guess I'm a fixer. If the default output isn't likely to be useful, I change it, typically by using a calculated property ( @{Label=" "; Expression = {}} ) or by using Update-TypeData to add a more useful property.

  4. Richard Siddaway

    I output data in the format that's most useful. Obvious things that need reformatting include WMI dates (get to love the CIM cmdlets), disk sizes and LastLogon times in AD for example

    Many of my scripts are used by non-PowerShell experts. I regard it as bad practice to output data in a format that requires them to do more work. So output is formatted to be most useful. Select calculated statements are good as is creating a new object type

Comments are closed.