PowerShell Write-Output [With Examples]

As a PowerShell developer, you should know how to use the Write-Output cmdlet, which allows you to send objects to the pipeline. In this tutorial, I will explain how to use the PowerShell Write-Output cmdlet with examples.

The Write-Output cmdlet in PowerShell is used to send objects to the pipeline to pass data between cmdlets or functions. Its basic syntax is Write-Output [-InputObject] <PSObject[]> [-NoEnumerate] [<CommonParameters>]. For example, Write-Output “Hello, USA” will send the string “Hello, USA” to the pipeline, allowing it to be processed by subsequent cmdlets.

What is PowerShell Write-Output?

The Write-Output cmdlet is used to send objects to the output stream in PowerShell. This cmdlet is particularly useful when you want to pass data down the pipeline to another cmdlet or function. Unlike Write-Host, which only displays output on the screen, Write-Output can be used to pass objects to other cmdlets for further processing.

Syntax of Write-Output

The syntax for Write-Output is:

Write-Output [-InputObject] <PSObject[]> [-NoEnumerate] [<CommonParameters>]
  • InputObject: Specifies the object to send down the pipeline.
  • NoEnumerate: Prevents the cmdlet from enumerating collections.

Write-Output writes the specified objects to the pipeline. If the command is the last one in the pipeline, the objects are displayed in the console.

Check out PowerShell Compare-Object

PowerShell Write-Output Examples

Now, let me show you some examples of how Write-Output can be used in PowerShell effectively.

Example 1: Passing Data to Another Cmdlet

In this example, we will use Write-Output to pass data to the Sort-Object cmdlet:

$data = "Washington", "California", "Texas", "New York"
$data | Write-Output | Sort-Object

This script sorts the states alphabetically and displays the sorted list.

Here is the exact output in the screenshot below:

PowerShell write-output

Example 2: Using Write-Output in a Function

Here’s an example of how Write-Output can be used within a function in PowerShell:

function Get-States {
    $states = "Washington", "California", "Texas", "New York"
    Write-Output $states
}

Get-States | ForEach-Object { Write-Output "State: $_" }

In this script, the Get-States function returns a list of states, which is then processed by ForEach-Object to prepend “State:” to each state name.

I executed the above script using VS code, and you can see the exact output in the screenshot below:

write-output in PowerShell

Example 3: Preventing Enumeration

By default, Write-Output enumerates collections. You can use the -NoEnumerate parameter to prevent this behavior. Here is a PowerShell script and example.

$states = "Washington", "California", "Texas", "New York"
Write-Output -NoEnumerate $states

This script will output the entire array as a single object rather than enumerating each state individually.

Check out PowerShell Write-Host vs Write-Error

PowerShell write-output color

In PowerShell, the Write-Output cmdlet itself does not support color customization. However, you can use the Write-Host cmdlet to achieve colored output.

Let me show you some examples.

The Write-Host cmdlet allows you to specify the color of the text and background using the -ForegroundColor and -BackgroundColor parameters.

Syntax

Write-Host -ForegroundColor <Color> -BackgroundColor <Color> -NoNewline
  • ForegroundColor: Specifies the color of the text.
  • BackgroundColor: Specifies the color of the background.
  • NoNewline: Prevents a new line after the output.

Example 1: Simple Colored Text

Here’s how to display text in green:

Write-Host "Success: Operation completed successfully." -ForegroundColor Green

This command will display the text “Success: Operation completed successfully.” in green.

Example 2: Text with Different Colors

You can combine multiple Write-Host commands to display text in different colors on the same line:

Write-Host "Warning:" -ForegroundColor Yellow -NoNewline
Write-Host " Disk space is low." -ForegroundColor Red

This will display “Warning:” in yellow and ” Disk space is low.” in red on the same line.

Example 3: Using Background Colors

You can also specify background colors:

Write-Host "Error: File not found." -ForegroundColor White -BackgroundColor Red

This command will display the text “Error: File not found.” with white text on a red background.

Check out PowerShell Write-Host vs Write-Information

PowerShell write-output no newline

In PowerShell, the Write-Output cmdlet always appends a newline at the end of the output, and it does not have an option to suppress this behavior directly. However, you can achieve similar functionality using the Write-Host cmdlet with the -NoNewline parameter.

The Write-Host cmdlet can be used to write text to the console without adding a newline at the end using the -NoNewline parameter.

Syntax

Write-Host "Text" -NoNewline
  • Text: The text you want to display.
  • -NoNewline: Prevents the cmdlet from adding a newline at the end of the output.

Example 1: Simple Output Without Newline

Here’s an example of how to print text without a newline:

Write-Host "Processing..." -NoNewline
Start-Sleep -Seconds 2
Write-Host " Done!"

This script will display “Processing…” and, after a 2-second pause, append ” Done!” on the same line.

Example 2: Create a Progress Indicator

You can use Write-Host with -NoNewline to create a simple progress indicator:

for ($i = 1; $i -le 10; $i++) {
    Write-Host "." -NoNewline
    Start-Sleep -Milliseconds 500
}
Write-Host " Completed!"

This script will print ten dots on the same line, followed by ” Completed!”

Conclusion

Write-Output in PowerShell helps you pass data down the pipeline for further processing. In this tutorial, I explained how to use the Write-Output PowerShell cmdlet with some real examples.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.