Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

powershell

What is the PowerShell Write-Host?

Martin Tiangco

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The Write-Host produces a customized displayed output to a host or console. We'll cover its syntax, inputs, outputs, and parameters. We'll wrap it up with a few examples.

Syntax

Write-Host
[[-Object] <Object>]
[-NoNewline]
[-Separator <Object>]
[-ForegroundColor <ConsoleColor>]
[-BackgroundColor <ConsoleColor>]
[<CommonParameters>]

Inputs

Instead of the -Object parameter, the object can alternatively be piped as an input to Write-Host.

Outputs

The Write-Host doesn't return any objects but displays them to the host or console.

Parameters

  • -BackgroundColor : This colors the background behind the text, which has no default value.
  • -ForegroundColor: This colors the text, which has no default color. The available colors are the same as the colors for -BackgroundColor.
  • -NoNewline: Applying this parameter will result in the next Write-Host call displaying its output next to the first call. No newline nor space is added after the first output.
  • -Object: This is the object(s) to display. If it is a collection, then its elements are separated by a space character. This can be overridden with the separator parameter. This can alternatively be supplied as an input to Write-Host.
  • -Separator: This is the separator string to insert between objects in the output.

Example 1

Write-Host ("Hello", "World") -ForegroundColor White -BackgroundColor DarkRed -separator ", "
Custom colored output with separator

Let's take a look at this line.

  • ("Hello", "World"): This is the object as the input. This will be printed according to the other parameters. Alternatively, we can supply this object by using the -Object parameter, i.e. -Object ("Hello", "World") .
  • -ForegroundColor White: This colors the text white.
  • -BackgroundColor DarkRed: This colors the background dark red.
  • -separator ", ": The separator between the elements "Hello" and "World" is a comma and a space character.

Try out the command in the terminal below.

Terminal 1
Terminal

Example 2

Write-Host "Hello" -NoNewline
Write-Host "World"
NoNewline

Let's take a look at this line.

  • "Hello" -NoNewline: This will print "Hello". The -noNewline parameter means this call of Write-Host will not add a newline at the end of its output. We can write multiple lines to PowerShell by using "shift" + "enter".
  • Write-Host "World":This will print "World".

Try out the command in the terminal below.

Terminal 1
Terminal

Example 3

Write-Host (1,2,3) 6> .\log.txt
Redirect host to a text file

Let's take a look at this line.

  • (1,2,3): It's the object which will be formatted with spaces between each number, i.e., "1 2 3".
  • 6>: This is the redirection operator which refers to the information stream. The output is "1 2 3" in a log.txt file, which will be created if it doesn't exist. The command (Get-ChildItem).FullNamecan be used to see the file created.
Terminal 1
Terminal

RELATED TAGS

powershell

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring