Powershell

Write-Host cmdlet in PowerShell

PowerShell scripting language supports all the basic functionalities of a programming language. PowerShell supports a number of cmdlets that include Write-Host, Write-Output, and echo for printing purposes.

The echo and Write-Output are used to display the text in a formatted way and return the value to the PowerShell engine. Write-Host, on the other hand, doesn’t print any value to the PowerShell console; it simply processes the data that is input on the screen.

The main objective of this article is to explore the PowerShell Write-Host cmdlet and its related useful functions.

How to use the Write-Host cmdlet in PowerShell?

In PowerShell, the Write-Host is used to display the output results on your terminal screen. Write-Host has different built-in parameters for various relevant tasks. The following are some brief examples of the use of Write-Host and its parameters in various scenarios.

Example 1: How to print a line using Write-Host cmdlet?

Primarily, the Write-Host cmdlet prints the output on the shell. The following script will be used, If you want to print the text on your terminal.

Clear-Host

$topic = Write-Host "Topic: Write-Host cmdlet in PowerShell. "

$topic

According to the above result snippet, first a string with WriteHost cmdlet is stored in a variable called $topic. When the $topic is called, it returns the string as it was defined.

Example 2: How to merge multiple lines using Write-Host cmdlet in PowerShell?

With the help of the -NoNewLine parameter you can merge the multiple lines into a single. The following code is given below as an example.

Clear-Host

Write-Host "Hello PowerShell! " -NoNewLIne

Write-Host "Topic: Write-Host cmdlet in PowerShell. "

The above output shows that the two different line strings have been merged into a single line.

Example 3: How to separate multiple strings using Write-Host cmdlet?

The “-Separator” operator defines output in a separated form. You can separate multiple strings into different parts with the use of defined separators. The example is given below.

Clear-Host

Write-Host ("Topic:", "Write-Host", "cmdlet", "in", "PowerShell.") -Separator " --> "

In the above code, the “–>” is used as a separator.

As you can observe from the above output, the multiple strings have been separated with a user-defined separator i.e. (–>)

Note: One thing to be noted, the Separator does not work with a single string, it applies only to multiple strings. See the example below.

Clear-Host

Write-Host ("Hello PowerShell! Topic: Write-Host cmdlet in PowerShell. ") -Separator " -> "

It can be noticed from the above output that the separator does not apply on a single.

Example 4: How to change the font color of the output using the Write-Host cmdlet?

The Write-Host can beautify your output Text with ForegroundColor. See the below example.

Clear-Host

Write-Host "Hello PowerShell! Topic: Write-Host cmdlet in PowerShell. " -ForegroundColor Green

The above example shows that the output text has changed to green color. You can also use more color options.

Example 5: How to change the background color of the output using the Write-Host cmdlet?

Write-Host provides facilities for coloring the background of the output text. Use the -BackgroundColor parameter with Write-Host to change the background color. The example code is given below.

Clear-Host

Write-Host "Hello PowerShell! Topic: Write-Host cmdlet in PowerShell. " -BackgroundColor Red

The above-mentioned output shows that with -BackgroundColor parameter the background color can be modified.

You have explored the main functions of the Write-Host cmdlet in PowerShell in this post.

Conclusion

Write-Host is a command used for displaying the output text in PowerShell. Unlike echo and Write-Output, it does not return any value to the console. This section demonstrates the working and usage of the Write-Host cmdlet in PowerShell. It does customize the output with the help of numerous operators. For instance, one can change the font color as well as the background of the output. Moreover, multiple strings can be separated using the Write-Host cmdlet.

About the author

Adnan Shabbir