powershell

PowerShell Write-Output is a default output behaviour. Why do we need it at all?


As described on Write-Output, all Write-Output does is sending the output to the current output device, which is a default anyway.

It appears that the only advantage of having explicitly used Write-Output is the ability to provide a parameter, -NoEnumerate. Is that correct?

If not, then what are other cases for using Write-Output?


Solution

  • I think the main advantage really comes down to readability of your code, as it becomes easy at a glance to see that you're sending something to the output.

    For instance, even with something as simple as

    $foo = "Current date is $(get-date)"
    

    I would argue that scanning through lines of code it's far easier to instantly understand what's happening when you see

    Write-Output $foo
    

    than if you just see

    $foo
    

    on a line of its own.

    It's perhaps worth noting that shortened forms appear to be going out of popularity, or no longer being recommended in the same way. For instance, if you edit PowerShell code with Visual Studio Code, you'll perhaps notice it showing warnings for the use of aliases, for instance, ft aliasing Format-Table, select aliasing Select-Object, etc., with the same listed reasoning that it makes it harder to maintain the code.