Home Tech How To Use PowerShell Write Host Command

How To Use PowerShell Write Host Command [Step-By-Step]


There is a lot of jargon and terminology in the computing world. Sometimes it becomes a bit tricky knowing what to use, keep or throw away. With PowerShell, the PowerShell write host command is one that you may not be sure of.

Straight out of my heart I would tell you, that using this command will depend on your needs – You may or may not ever need to use it.

The article will help inform your decision on using this command. You will understand the PowerShell Write-Host and know when it must be used.

Without further ado, let us jump right into it!

How to Use the Modern PowerShell Write Host Command

Emphasis on the word “modern”. The reason is before PowerShell 5.x, With PowerShell write host was a bit different. It had no links to stream (such as error, success, or warning). This meant the use of this command was a little bit discouraged, mainly because you did not have as much control in relation to other output types.

It is not uncommon in Powershell scripts for information to be passed down to a user. This information may not directly relate to the script’s output, but it is still informational.

I will show with a small example, a command that dishes information to a user with regards to the processing time of a script.

Write-Host “Let us gather some important information now. Be patient, it will take a minute!”
Write-Host “- Gathering Process`r`n- Gathering Files`r`n- Gathering Users”
Write-Output “This is a place holder for the information”
Write-Host “Now the Information Gathering has been completed!”

powershell write host

If you take a close look at the command that was run and the final executed script, you will see that it simply serves the purpose of outputting some information to the host. In this example, the host happens to be the end-user of the script.

The bigger question would be if it is possible to display information when you send sending objects between different functions or cmdlet. The answer is YES! PowerShell’s Write Host function has no effect on the pipeline output. Below is an example of how this works.

Function Get-Information {
    Write-Host “Let us gather some important information now. Be patient, it will take a          minute!`r`n”
    Write-Host “- Gathering Data`r`n- Returning Data`r`n”

      “Test1” = “Value1”
      “Test2” = “Value2”
   Write-Host “Information Gathering Complete!”
     Function Convert-Information {
  $Param | ConvertTo-JSON

It is possible to pipe a Get-Information output to Convert-Information. Doing this means is ceases to take into cognizance the different statements and will only act upon the PSCustomObject that is rendered as output.

How to Format Messages to Make Them More Readable

A message can be plain or beautifully presented. PowerShell write host is capable of letting users format their messages, adding color to the output. Doing this helps in increasing the readability. It will help anyone using the script understand better the actions to take.

Here are a few format options to keep in mind:

  1. BackgroundColor: This is the color of what you have behind the text.
  2. ForegroundColor: This refers to the color of the text itself.
  3. NoNewLine: This simply commands the computer not to show a new line after the command is executed.
  4. Separator: This is a good way to show word or character separators that are not the regular space used by Write-Host. This may refer to a newline character or any carriage return.

Next, I would like to show you all the options you would be able to harness in constructing a beautifully formatted message.

I will be creating an informational message using an array for the number of results. In this example, I would apply some formatting to make it pop a bit more.

Note: It is possible to pass several objects in a With PowerShell Write Host command just as is displayed below. Ideally, there would be a need to have a scripter to create a separation in these messages, but using the separator parameter, it is possible to control how the output shows up on the console.

$Array = @(5,6,7,8,9)

Write-Host “Count Results:” -BackgroundColor DarkBlue -ForegroundColor White -NoNewline
Write-Host “” $Array.Count “Items`r`n” -ForegroundColor Red
Write-Host $Array -Separator “`r`n”
Write-Host “” “End of results.” -ForegroundColor Green -Separator “`r`n”

powershell write host

By performing a format as we have in the above example, the user now has more useful information about the script to work with. One use case would be if an informational menu needs to be created. There are of course many other use cases.

How to Redirect Output from PowerShell Write Host Stream

There are a number of methods for controlling and diverting the output now that With PowerShell Write Host may be controlled by stream output. With PowerShell Write Host messages, the InformationAction choice is partially taken into consideration.

Although the -InformationAction Ignore option successfully suppresses Write-Host messages, the $InformationPreference and the InformationAction parameters have no impact on the Write-Host cmdlet.

Write-Host is nothing more than a wrapper for the Write-Information function. This allows Write-output Hosts to be included in the information stream. This implies you can use the information stream to change where Write-Host messages travel.

However, you may still use the redirection operators to send the output of Write-Host messages to a separate stream. Write every Write-Host message to a log file, for example. Let’s use the same example as before and redirect all of the output to a log file.

Write-Host “Counting Results:” -BackgroundColor DarkBlue -ForegroundColor White -NoNewline 6>> .\logfile.txt
Write-Host “” $Array.Count “Items`r`n” -ForegroundColor Yellow 6>> .\logfile.txt
Write-Host $Array -Separator “`r`n” 6>> .\log.txt
Write-Host “” “Results End Here.” -ForegroundColor Red -Separator “`r`n” 6>> .\logfile.txt

How To Use PowerShell Write Host Command [Step-By-Step]

PowerShell now routes the result to a log, as you’ve seen, although the style may not be precisely what you wanted. Because the -NoNewLine option is mainly used as a host styling tool, it isn’t actually honored. This isn’t a major problem, but it’s something to consider when diverting the stream output.

What is Powershell

If you have read this far, you must have an understanding of PowerShell. but I would like to round up with a quick sum-up of the tool where you can execute the With PowerShell write host command we have discussed.

PowerShell is a task automation system that consists of a console or command-line shell, scripting or programming language, and a configuration management structure that works across platforms. PowerShell is compatible with Linux, Windows, and macOS.

PowerShell is a contemporary command shell that combines the finest aspects of existing common command shells. PowerShell takes and returns.NET objects, unlike other shells that recognize and return text. These features are included in the shell:

  • Command-line history that is robust
  • Command prediction and Tab completion
  • Script and parameter aliases are supported
  • The in-console help system, comparable to the Unix man pages
You may like to read our guide on PowerShell vs Command Prompt: Getting Started with Windows PowerShell.

Language for scripting

PowerShell is a language for scripting that is widely used to automate system management and administration. It’s also utilized in CI/CD setups to create, test, and deliver solutions. The.NET Common Language Runtime underpins PowerShell (CLR).

.NET objects are used for all inputs and outputs. There usually will not be a need to parse text output for the purpose of getting information out of it. The following are some of the characteristics of the PowerShell scripting language:

  • Functions, scripts, classes, and modules make it extensible.
  • Formatting method with a lot of flexibility for simple output
  • Dynamic type system with an extensible type system
  • Support for standard data formats such as JSON, CSV, and XML is built-in.


Write-Host is fully permitted for usage and recommended when you’re about to show informative messages to the user, thanks to the improvements in PowerShell 5.x and on. Because a PowerShell user has the same degree of control over an informational message as they have over an outgoing or verbose message, using With PowerShell Write Host or Write-Information is advised and recommended.

As always, I like to get feedback from you. In the comment section, drop some of the ways you have used the PowerShell Write Host command.