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.
Write-Host[[-Object] <Object>][-NoNewline][-Separator <Object>][-ForegroundColor <ConsoleColor>][-BackgroundColor <ConsoleColor>][<CommonParameters>]
Instead of the -Object parameter, the object can alternatively be piped as an input to Write-Host.
The Write-Host doesn't return any objects but displays them to the host or console.
-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.Write-Host ("Hello", "World") -ForegroundColor White -BackgroundColor DarkRed -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.
Write-Host "Hello" -NoNewlineWrite-Host "World"
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.
Write-Host (1,2,3) 6> .\log.txt
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.