To use or not to use Write-Output in functions

This topic contains 0 replies, has 1 voice, and was last updated by Profile photo of Forums Archives Forums Archives 5 years, 6 months ago.

  • Author
    Posts
  • #6052

    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:

    [quote]New-Object -TypeName PSObject -Property $props
    }
    [/quote]

    ..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! – http://technet.microsoft.com/en-us/maga ... 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: http://powershell.org/discuss/viewtopic.php?f=2&t=53

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

    Thanks for pointing me there.

You must be logged in to reply to this topic.