To use or not to use Write-Output in functions

Welcome Forums General PowerShell Q&A To use or not to use Write-Output in functions

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 7 years ago.

  • Author
  • #6052

    Points: 0
    Rank: Member

    by jeremytbradshaw at 2013-03-10 06:14:39

    I noticed in the "Creating HTML Reports in PowerShell" scripts, all of the functions end with:

    New-Object -TypeName PSObject -Property $props

    ..rather than:

    $obj = New-Object -TypeName PSObject -Property $props
    Write-Output $obj

    From googling a bit on this I think the difference between the two is how Write-Output handles $null vs. the pipeline, but I would imagine Write-Output's dropping of $null as input would be preferable over the pipeline. Can anyone who knows the reason(s) to use one method over the other please shed some light on it?

    I did search to see if this was already asked but couldn't find anything.

    Thanks in advance.

    PS. oh and by the way – many thanks for all of the amazing help in the free ebooks / scripts / this forum!

    by jeremytbradshaw at 2013-03-10 10:31:27

    I found this! – ... 50381.aspx. And in there, Don Jones explains what storing the new custom object in a variable can be handy for, such as if you want to later modify the object (e.g. adding a custom TypeName to it).

    Feel free to close this one unless somebody has more input.

    by MattG at 2013-03-12 15:57:55

    Hi Jeremy,

    I posed this question a while back and got some good responses from Don Jones and Jeff Hicks:

    by jeremytbradshaw at 2013-03-17 01:46:42

    Thanks for pointing me there.

The topic ‘To use or not to use Write-Output in functions’ is closed to new replies.