Write to the Console in Powershell

PowerShell has various methods to write output to the console. This article discusses 4 of these methods:

  • Using the Write-Output cmdlet,
  • Using the Write-Host cmdlet,
  • Using the Write-Error cmdlet, and
  • Using System.Console class,

Before we discuss those methods, let’s briefly discuss how pipelines work in PowerShell. It is essential to understand this before we go further.

About Pipelines in PowerShell

A PowerShell pipeline is a series or chain of commands connected by the pipeline operator (“|”). Each pipeline operator sends the previous command’s output to the next command.

Here is a syntax of a pipeline:

In that syntax, the output of <command-1> is sent to <command-2>. <command-2> processes the input objects and sends the result to <command-3>, which processes the input and passes the output to the following command in the pipeline, and so on.

Important Note: When the end of the pipeline is reached, the output of the last command is written to the console by default.

For example,

In the example above, the whole command is a chain of three commands. The first command in the pipeline (Get-Service) lists all installed services.

The list is sent down the pipeline to the Where-Object to filter the list and output the running processes only. The result is then sent to the last command for sorting. After the last command, PowerShell dumps the result into the console.

Letting things fall out of the pipeline into the console doesn’t dismiss the need to understand the methods of writing output to the console. The techniques discussed here will give you control over how you write output to the console.

Method 1: Using Write-Output Cmdlet

This command writes the result to the pipeline. If the cmdlet is the last command in the pipeline, the result is displayed in the console.

The following code examples show how to write an object into the console with Write-Output.

Next, an example showing that Write-Output can pass data through a pipeline stream.

Output (formatted for better viewing):

-4, 1, 6, 18

In the example above, the Write-Output sends the given array to the Sort-Object cmdlet for sorting.

Method 2: Write-Host

Unlike Write-Output, which can send data through the pipeline, Write-Host has the sole purpose of writing the output to the console.

As shown in the output, Write-Host printed the array into the console without sending it to the Sort-Object cmdlet.

Write-Host also allows the user to customize the data printed on the host application. The following example shows how to change font color using -ForegroundColor and background color with the -BackgroundColor parameter.

You can see more customization options for Write-Host in the documentation.

Method 3: Raising Exception with Write-Error

Write-Error cmdlet is used to raise a non-terminating error and write it to the console. By non-terminating, we mean that the error thrown by this cmdlet does not stop the execution of the rest of the code.

The following code shows PowerShell code to check whether a given folder exists. If the folder does not exist, we raise an error using Write-Error

Output (on the console):

Write-Error: Folder doesn't exist.

Method 4: Using [System.Console] Class

This .NET class system writes output into the console using Write() or WriteLine() methods.

For example,

Another example for writing numbers 1 through 10 using the Write() and WriteLine() methods.

Output:

Notice that the difference between Write() and WriteLine() is that each value on the latter is followed by a new line character, by default.

Here is a last example with placeholders for values we want to print.

Output:

Hello
World

Conclusion

This article discussed four methods for writing output into the console. We have seen that Write-Host (See Method 2) is the cmdlet whose sole purpose is to display results in the output. On the other hand, Write-Output writes output into the console and can send data to the pipeline stream.

We also discussed using Write-Error to raise a non-terminating error and [System.Console] class for writing to the standard output stream.