Powershell

How to Use the “Write-Output” Cmdlet in PowerShell?

The “Write-Output” cmdlet in PowerShell is utilized to write or output the specified list of objects to the pipeline. In case, if the stated cmdlet is the last in the pipeline, then the objects will be displayed in the console’s output. Typically, it outputs or sends the object in the primary pipeline, which is also known as the “output stream”. Moreover, it can be utilized in scripts to send commands.

In this write-up, the cmdlet “Write-Output” will be demonstrated in PowerShell.

How to Use the Cmdlet “Write-Output” in PowerShell?

As stated above, the “Write-Output” cmdlet is used for displaying the output in the PowerShell console. Its primary purpose is to send the objects to the pipeline.

Now, check out the below-provided examples for a practical demonstration of the stated cmdlet!

Example 1: Utilize the Cmdlet “Write-Output” to Write Text in PowerShell Console
To write the text in the PowerShell console, use the “Write-Output” cmdlet:

Write-Output "This is Linux Hint portal."

Example 2: Use the “Write-Output” Cmdlet to Display the Output of the Cmdlet Assigned Variable
Use the provided demonstration for displaying the output of the cmdlet assigned variable:

$var = Get-Service
Write-Output $var

According to the above code:

  • Initialize a variable “$var” variable and assign it the “Get-Service” cmdlet.
  • Use the “Write-Output” cmdlet along with the “$var” assigned variable:

Example 3: Use the “Write-Output” Cmdlet to Display the Total Number of Values
To write the output and measure the objects in the console, execute the below-stated cmdlet:

Write-Output 4,5,6,7 | Measure-Object

In the above-illustrated code:

  • Write the “Write-Output” cmdlet along with the numbers separated by commas.
  • Then, add the “|” pipeline and assign the “Measure-Object” cmdlet:

Example 4: Use the “Write-Output” Cmdlet to Write Text in a PowerShell Script
This example will illustrate the procedure to write the text output in a PowerShell script with the cmdlet “Write-Output”. For that corresponding purpose, simply add the “Write-Output” cmdlet along with the desired text that needs to be displayed as an output:

Write-Output "This is a sample text."

That was all about the working of the “Write-Output” cmdlet in PowerShell.

Conclusion

The cmdlet “Write-Output” in PowerShell is utilized for writing objects to the pipeline. Primarily, it is considered the “go-to” command for writing output in the console. This write-up has demonstrated the cmdlet “Write-Output” in detail.

About the author

Muhammad Farhan

I am a Computer Science graduate and now a technical writer who loves to provide the easiest solutions to the most difficult problems related to Windows, Linux, and Web designing. My love for Computer Science emerges every day because of its ease in our everyday life.