Write-Output (Microsoft.PowerShell.Utility) - PowerShell (original) (raw)
- Reference
Writes the specified objects to the pipeline.
Syntax
Write-Output
[-InputObject] <PSObject[]>
[-NoEnumerate]
[<CommonParameters>]
Description
Writes the specified objects to the pipeline. If Write-Output
is the last command in the pipeline, the objects are displayed in the console.
Write-Output
sends objects to the primary pipeline, also known as the success stream. To send error objects to the error stream, use Write-Error
.
This cmdlet is typically used in scripts to display strings and other objects on the console. One of the built-in aliases for Write-Output
is echo
and similar to other shells that use echo
. The default behavior is to display the output at the end of a pipeline. In PowerShell, it is generally not necessary to use the cmdlet in instances where the output is displayed by default. For example,Get-Process | Write-Output
is equivalent to Get-Process
. Or, echo "Home directory: <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>H</mi><mi>O</mi><mi>M</mi><mi>E</mi><mi mathvariant="normal">"</mi><mi mathvariant="normal">‘</mi><mi>c</mi><mi>a</mi><mi>n</mi><mi>b</mi><mi>e</mi><mi>w</mi><mi>r</mi><mi>i</mi><mi>t</mi><mi>t</mi><mi>e</mi><mi>n</mi><mo separator="true">,</mo><mi mathvariant="normal">‘</mi><mi mathvariant="normal">"</mi><mi>H</mi><mi>o</mi><mi>m</mi><mi>e</mi><mi>d</mi><mi>i</mi><mi>r</mi><mi>e</mi><mi>c</mi><mi>t</mi><mi>o</mi><mi>r</mi><mi>y</mi><mo>:</mo></mrow><annotation encoding="application/x-tex">HOME"
can be written, "Home directory: </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.8889em;vertical-align:-0.1944em;"></span><span class="mord mathnormal" style="margin-right:0.08125em;">H</span><span class="mord mathnormal" style="margin-right:0.05764em;">OME</span><span class="mord">"‘</span><span class="mord mathnormal">c</span><span class="mord mathnormal">anb</span><span class="mord mathnormal">e</span><span class="mord mathnormal" style="margin-right:0.02691em;">w</span><span class="mord mathnormal" style="margin-right:0.02778em;">r</span><span class="mord mathnormal">i</span><span class="mord mathnormal">tt</span><span class="mord mathnormal">e</span><span class="mord mathnormal">n</span><span class="mpunct">,</span><span class="mspace" style="margin-right:0.1667em;"></span><span class="mord">‘"</span><span class="mord mathnormal">Ho</span><span class="mord mathnormal">m</span><span class="mord mathnormal">e</span><span class="mord mathnormal">d</span><span class="mord mathnormal">i</span><span class="mord mathnormal">rec</span><span class="mord mathnormal">t</span><span class="mord mathnormal" style="margin-right:0.03588em;">ory</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">:</span></span></span></span>HOME"
.
By default, Write-Output
enumerates objects in a collection. However, Write-Output
can also pass collections down the pipeline as a single object with the NoEnumerate parameter.
Examples
Example 1: Get objects and write them to the console
In this example, the results of the Get-Process
cmdlet are stored in the $P
variable. TheWrite-Output
cmdlet displays the process objects in $P
to the console.
$P = Get-Process
Write-Output $P
Example 2: Pass output to another cmdlet
This command pipes the "test output" string to the Get-Member
cmdlet, which displays the members of the System.String class, demonstrating that the string was passed along the pipeline.
Write-Output "test output" | Get-Member
Example 3: Suppress enumeration in output
This command adds the NoEnumerate parameter to treat a collection or array as a single object through the pipeline.
Write-Output 1,2,3 | Measure-Object
Count : 3
...
Write-Output 1,2,3 -NoEnumerate | Measure-Object
Count : 1
...
Parameters
-InputObject
Specifies the objects to send down the pipeline. Enter a variable that contains the objects, or type a command or expression that gets the objects.
Type: | PSObject[] |
---|---|
Position: | 0 |
Default value: | None |
Required: | True |
Accept pipeline input: | True |
Accept wildcard characters: | False |
-NoEnumerate
By default, the Write-Output
cmdlet always enumerates its output. The NoEnumerate parameter suppresses the default behavior, and prevents Write-Output
from enumerating output. TheNoEnumerate parameter has no effect if the command is wrapped in parentheses, because the parentheses force enumeration. For example, (Write-Output 1,2,3 -NoEnumerate)
still enumerates the array.
The NoEnumerate parameter is only useful within a pipeline. Trying to see the effects ofNoEnumerate in the console is problematic because PowerShell adds Out-Default
to the end of every command line, which results in enumeration. But if you pipe Write-Output -NoEnumerate
to another cmdlet, the downstream cmdlet receives the collection object, not the enumerated items of the collection.
Type: | SwitchParameter |
---|---|
Position: | Named |
Default value: | None |
Required: | False |
Accept pipeline input: | False |
Accept wildcard characters: | False |
Inputs
You can pipe objects to this cmdlet.
Outputs
This cmdlet returns the objects that are submitted as input.
Notes
PowerShell includes the following aliases for Write-Output
:
All platforms:
echo
Windows:
write