Write-Output
suggest changeWrite-Output writes objects to the success stream. The next command in the pipeline receives them. If nothing does, the host displays them.
This is the success stream, also called the output stream. It is not the error stream. Use Write-Error for error records.
# 1.) Output to the next Cmdlet in the pipeline
Write-Output 'My text' | Out-File -FilePath "$env:TEMP\Test.txt"
Write-Output 'Bob' | ForEach-Object {
"My name is $_"
}
# 2.) Output to the console since Write-Output is the last command in the pipeline
Write-Output 'Hello world'
# 3.) No Write-Output cmdlet. A bare value is still written to the success stream.
'Hello world'
- The Write-Output cmdlet sends the specified object down the pipeline to the next command.
- If the command is the last command in the pipeline, the object is displayed in the console.
- The PowerShell interpreter treats this as an implicit Write-Output.
The host displays whatever is left on the success stream, so Write-Output is often unnecessary. Get-Process | Write-Output is equivalent to Get-Process.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents