3

Let's see short PS-snippets:

Write-Output @(1,2,3)
Write-Output @(1,2,3).Count
Get-Process|Write-Output
Write-Output (Get-Process)
$p=7890;Write-Output "Var. is $p"
Write-Output "ABCD78".Length

Function Get-StoppedService {

Param([string]$Computername = $env:computername)

$s = Get-Service -ComputerName $Computername
$stopped = $s | where {$_.Status -eq 'Stopped'}
Write-Output $Stopped
}
Get-StoppedService

In all snippets above I can just wipe out string Write-Output (with trailing space, if any) and will have exactly the same functionality. The question is: do you know example where we CAN'T throw off Write-Output? Of course, if you interesting in Write-Output's parameter -NoEnumerate you can't eliminate cmdlet itself, so let's suppose we don't want/need -NoEnumerate. In this case - do you have example?

Smarty
  • 1,579
  • 2
  • 11
  • 16
  • 1
    You may redefine `Write-Output`. I did not see much applications of this, but this is a possibility to keep in mind. – Roman Kuzmin Jul 08 '16 at 18:13
  • PetSerAl, you just "trick" PS-engine. You try to create array of strings and print it's elements. According PowerShell Language Specification(ver. 3.0) only two variants are STRICTLY syntactical valid: write "a","b","c","d","e" or write 'a','b','c','d','e'. In both cases you can wipe out "write" and get what you want. – Smarty Jul 10 '16 at 15:48

2 Answers2

3

Well MSDN itself has the the following statement

This cmdlet is typically used in scripts to display strings and other objects on the console. However, because the default behavior is to display the objects at the end of a pipeline, it is generally not necessary to use the cmdlet. For example, get-process | write-output is equivalent to get-process.

One case in which it might be useful is when you build a pipeline with unknown stages, i.e. stages which are given to you via parameters etc. and the user could give you any cmdlet for the stage e.g. Tee-Object. If the user don't want anything special to happen, he could simply pass Write-Output as a kind of "pass-through" stage (of course you could easily implement that yourself as well)

DAXaholic
  • 33,312
  • 6
  • 76
  • 74
1

As is often the case, PetSerAl's pithy examples (by way of a comment on the question, in this case) lead to epiphanies:

While generally not needed (see DAXaholic's helpful answer; you don't even need Write-Output for -NoEnumerate, because that can be emulated with the unary array-construction operator, ,), Write-Output can provide syntactic sugar:

(Write-Output a b c d e | Measure-Object).Count yields 5, which demonstrates that Write-Output accepts an arbitrary number of individual arguments, which are sent through the pipeline / to the screen one by one.

As such, Write-Output can simplify sending a collection of items to the output stream, by not requiring them to be quoted, because, as a cmdlet, Write-Output's parameters are parsed in argument mode:

Thus, instead of a "noisy" array literal such as the following (with its need for quoting the elements and separating them with ,):

"a", "b", "c", "d", "e"

you can simply write (albeit at the expense of performance):

Write-Output a b c d e
mklement0
  • 382,024
  • 64
  • 607
  • 775