Powershell

How to Write to the Console in PowerShell?

The PowerShell console displays the output of the executed commands, such as text, integers, or variables. More specifically, various commands are used to write the output to the PowerShell console. The core purpose behind this functionality is to display the added text on the terminal. Moreover, PowerShell also permits you to customize the text and the background color.

This post will overview the methods to write output to the PowerShell Console.

How to Write to the Console in PowerShell?

These cmdlets can be utilized to write to the PowerShell console:

Method 1: Write to the Console in PowerShell Using “Write-Output” Command

The “Write-Output” command is primarily used to output the text input to the console. This command sends the specified objects to the output stream.

Syntax

> Write-Output "sample text"

Example 1: Using “Write-Output” Command to Display a Message in PowerShell
In this example, we will output the added text by utilizing the “Write-Output” command:

> Write-Output "This is a sample text."

Example 2: Using “Write-Output” Command Inside the Function in PowerShell
For the second scenario, we will add the same command within a function like this:

function text-output {
    Write-output "This is a sample text"
}
text-output

In the above code:

  • First, we have defined a function named “text-output”.
  • After that, we added the “Write-Output” command to write the text to the PowerShell console.
  • Finally, call the function by writing its name outside the body:

The text has been written in the PowerShell ISE console.

Method 2: Write to the Console in PowerShell Using “Write-Host” Command

Another command used to write to the PowerShell console is the “Write-Host”. This cmdlet is used to output the text input to the PowerShell console. Moreover, it can be utilized for customizing the output text.

Example 1: Using “Write-Host” Command to Display a Message in PowerShell
Now, we will write the customized text to the PowerShell console:

> Write-Host "Hello People" -BackgroundColor Black

In the above code, we have used the “Write-Host” command to write the text to the PowerShell console. Moreover, the “-BackgroundColor” parameter is added to change the background color of the text to black:

Example 2: Using “Write-Output” Command Inside the Function in PowerShell
This example will output the customized text to the PowerShell ISE console:

function output-text {
    Write-Host "Hello People" -BackgroundColor Yellow
}
output-text

In the above code:

  • Define a function and then added the text using the “Write-Host” command.
  • Use the “-BackgroundColor” parameter to change the text color to yellow.
  • Finally, call the function with its name:

As you can observe, the customized text has been written in the PowerShell ISE console.

Conclusion

To write to the console in PowerShell, there are various methods, but the majorly used ones include the “Write-Output” and the “Write-Host” command. Both cmdlets take the input as text and output to the console. However, the Write-Host can also customize the added text. This post has explained practical methods to resolve the mentioned query.

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.