Thursday, August 11, 2022
HomeGadgetPowerShell Write-Host Defined: Syntax, Parameters, And Examples

PowerShell Write-Host Defined: Syntax, Parameters, And Examples


Are you curious to learn to use PowerShell Write-Host to write down output to a bunch? This text shall be your information.

This text begins with a fast overview of the Write-Host cmdlet. Then, within the second part, you’ll be taught the cmdlet’s syntax.

Following that, I clarify the parameters of Write-Host. Then, I’ve included ten examples in part 4 that can assist you learn to use this cmdlet.

Lastly, in case you nonetheless have any unanswered questions, you will get solutions to questions individuals principally ask about PowerShell Write-Host.

PowerShell Write-Host: Overview

PowerShell Write-Host: Overview

Have you ever ever written a PowerShell script, and it is advisable show some messages on the console? The Write-Host cmdlet is your man!

You should use this simple-to-use cmdlet to show any message on the PowerShell host or console.

This cmdlet has some helpful options. For instance, you may specify the colour of the textual content through the use of the ForegroundColor parameter.

Equally, you may specify the background colour utilizing the BackgroundColor parameter (extra on parameters within the third part).

Earlier than I transfer on, I like to say that beginning in Home windows PowerShell 5.0, Write-Host is a wrapper for Write-Data.

Syntax Of PowerShell Write-Host

Syntax Of PowerShell Write-Host

The Write-Host cmdlet has only one syntax. Right here it’s…

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

Parameters Of PowerShell Write-Host

Parameters Of PowerShell Write-Host

I’ve supplied an in depth rationalization of the Write-Host cmdlet parameters within the desk beneath.

S/N Parameter Identify Write-Host Parameter That means/Notes
1 Object You employ the Object parameter to specify the objects (texts) you want to show within the host or console. This parameter is in place zero, so you do not want to specify it earlier than including the objects (texts) you need to be displayed on the console.
2 NoNewline By default, the string representations of the enter objects are concatenated to type the output. Nonetheless, while you specify the NoNewline parameter, PowerShell Write-Host is not going to add areas or newlines between the output strings. Moreover, no new line is added after the final output string.
3 Separator If you wish to insert a separator string between objects displayed by the host, use the Separator parameter to specify the separator string to insert.
4 ForegroundColor You employ this parameter to specify the textual content colour of the item you need to be displayed on the host. There is no such thing as a default textual content colour. You’ll be able to specify the next values for the ForegroundColor parameter – Black, DarkBlue, DarkGreen, DarkCyan, DarkRed, DarkMagenta, DarkYellow, Grey, DarkGray, Blue, Inexperienced, Cyan, Pink, Magenta, Yellow, and White
5 BackgroundColor You employ the BackgroundColor parameter to specify the background colour of the textual content (object) you need to show on the host. You should use the identical colours listed within the ForegroundColor parameter above.
6 CommonParameters Write-Host helps PowerShell CommonParameters like ErrorAction, ErrorVariable, and the remaining. To be taught extra about PowerShell widespread parameters, go to the about_CommonParameters web page. Alternatively, you may run get-help about_CommonParameters command in PowerShell.

PowerShell Write-Host Examples

PowerShell Write-Host Examples

On this part, you’ll be taught other ways to make use of the Write-Host cmdlet. I’ve featured ten examples and purposes of Write-Host.

In my first instance beneath, I totally different Write-Host and Write-Output and in addition supply an instance.

How To Use PowerShell Write-Host vs Write-Output

The elemental distinction between Write-Host and Write-Output is how PowerShell processes the knowledge from the cmdlets. Whereas Write-Host writes to the console itself, Write-Output writes to the pipeline.

Why does this matter?

Effectively, since Write-Host doesn’t write to the pipeline, you can not feed its output into the following command by way of the pipeline. Quite the opposite, as a result of Write-Output writes to the pipeline, you should use its output to the following command by way of the pipeline.

To make the distinction clearer, I’ll exhibit with the next instance.

$ProcessID = (Get-Course of -Identify ACCSvc).Id
$ProcessName = (Get-Course of -Identify ACCSvc).ProcessName
$Handles = (Get-Course of -Identify ACCSvc).Handles

I can use the command beneath to write down the knowledge “The method, ACCSvc with ID 5492 has 86 handles”

Write-Host "The method, $ProcessName with ID $ProcessID has $Handles handles"

Nonetheless, I will be unable to ship this data to a textual content file by piping the output to the Add-Content material command.

Then again, Write-Output shows the identical message on the host (console). Then, pipe the end result (in the identical command) to Add-Content material, including the identical data to a textual content file.

Write-Output "The method, $ProcessName with ID $ProcessID has $Handles handles" | Tee-Object -FilePath D:reportWrite-Output.txt

So, the final command shows the knowledge on the console and sends it to a textual content file.

To get Write-Output to show the message on the console and ship it to a textual content file, I piped the results of the Write-Output to the Tee-Object command.

How To Use PowerShell Write-Host vs Write-Output

How To Add New Line With PowerShell Write-Host

By default, the PowerShell Write-Host doesn’t add a brand new line when it writes objects to the console. Relatively, it shows all the pieces in a single line.

Earlier than I present you find out how to use Write-Host so as to add a brand new line, let me first present you a normal command.

In my final instance, I used this set of instructions to generate the knowledge I wanted to write down to the host. Then, I used the second command to write down the knowledge to the host.

$ProcessID = (Get-Course of -Identify ACCSvc).Id
$ProcessName = (Get-Course of -Identify ACCSvc).ProcessName
$Handles = (Get-Course of -Identify ACCSvc).Handles
Write-Host "The method, $ProcessName with ID $ProcessID has $Handles handles"

Right here is the results of the final command…

How To Add New Line With PowerShell Write-Host

Shifting on to this instance’s subsequent stage, I’ll now write the next texts to the following line “Thanks for utilizing my script.”

To realize this, I’ll add the next code between the textual content I need to be displayed within the first line and the textual content I need to be displayed within the second line…

`r`n

In impact, r provides a carriage return whereas `n character provides a brand new line. Nonetheless, you want each characters so as to add the carriage return and the brand new line.

Right here is the ultimate code the place I exploit Write-Host so as to add a brand new line once I write output to host…

Write-Host "The method, $ProcessName with ID $ProcessID has $Handles handles `r`nThank you for utilizing my script."

When you look carefully, I didn’t add an area between `r`n characters and the textual content I would like PowerShell Write-Host to show within the second line.

Right here is the results of the command…

How To Use PowerShell Write-Host To Write Data From A Variable

To point out find out how to use Write-Host to write down data saved in a variable, I’ll use the next instructions:

$DisplayName = (Get-Service -Identify VSS).DisplayName
$Identify = (Get-Service -Identify VSS).Identify
Write-host "The show identify of $Identify is $DisplayName"

The primary command saves the show identify of a Home windows service to a variable I referred to as DisplayName. Equally, within the second command, I saved the identify of the service in one other variable – Identify.

Lastly, I used the Write-Host command to write down the knowledge saved within the two variables to host. Right here is the results of the final command.

How To Specify The Textual content Coloration With The ForegroundColor Parameter Of PowerShell Write-Host

On this instance, you’ll learn to change the colour of the textual content you show with Write-Host. The command has a parameter referred to as ForegroundColor.

On this instance, I need to change the colour of the textual content on this command to Magenta.

$DisplayName = (Get-Service -Identify VSS).DisplayName
$Identify = (Get-Service -Identify VSS).Identify
Write-host "The show identify of $Identify is $DisplayName" -ForegroundColor Magenta

If you write a script that shows a number of data on the console, altering the textual content colour improves the person expertise. Right here is the results of the command…

How To Specify The Textual content Background Coloration With The BackgroundColor Parameter Of PowerShell Write-Host

Within the final instance, I confirmed you find out how to change textual content colour while you write to the host. On this instance, you’ll learn to change the background colour.

Right here, you want the BackgroundColor Parameter as an alternative.

$DisplayName = (Get-Service -Identify VSS).DisplayName
$Identify = (Get-Service -Identify VSS).Identify
Write-host "The show identify of $Identify is $DisplayName" -BackgroundColor Magenta

Now PowerShell Write-Host used the default textual content colour, white. Then, it modified the textual content background to Magenta.

How To Specify The Text Background Color With The BackgroundColor Parameter Of PowerShell Write-Host

How To Write The Tab Character (`) With PowerShell Write-Host

In PowerShell, the tab character (`) is a particular character. Subsequently, in case you run the command write-host “`” in PowerShell, it is not going to show the tab character (`) as anticipated.

As an alternative, it’ll show >>; indicating that it was anticipating further enter after the tab character.

So, how do you utilize Write-Host to write down the tab character (`) to the PowerShell host?

One method to obtain that is to first save the tab character (`) in a variable utilizing single quotes, like this:

$tab = '`'

Then, use Write-Host to name the variable as proven within the command beneath…

write-host $tab

You may also add different strings to the write-host command. Right here is an instance…

write-host "That is the tab character written with write-host $tab"

…and listed below are the outcomes of the final two instructions in PowerShell…

How To Write The Tab Character (`) With PowerShell Write-Host

How To Concatenate Strings In PowerShell When Written With Write-Host

When you’ve got objects from a variable and want to mix them with a string, for instance, sprint (-), then ship it to the host with Write-Host, it will get sophisticated.

For instance, in my earlier examples, I used the next instructions:

$DisplayName = (Get-Service -Identify VSS).DisplayName
$Identify = (Get-Service -Identify VSS).Identify
Write-host "The show identify of $Identify is $DisplayName"

The command works as anticipated.

But when I run a single command that saves the output of “Get-Service -Identify VSS” in a variable $VSS. Then, if I exploit the second command beneath to write down data to the host…

$VSS = Get-Service -Identify VSS
Write-host "The show identify of $VSS.Identify is $VSS.DisplayName"

…this command is not going to show the anticipated data.

To unravel the issue, I’ll modify the command as proven beneath.

Write-host "The show identify of $($VSS.Identify) is $($VSS.DisplayName)"

The one change I made to the final command is including the $ character earlier than $VSS.Identify and $VSS.DisplayName

Now the command shows the knowledge as anticipated.

You’ll be able to even make additional modifications to the knowledge. For instance, it’s possible you’ll add one other string. for instance, sprint (-), as proven within the command beneath:

Write-Host "The show identify of $($VSS.Identify) - $($VSS.DisplayName)"

How To Cross Variable In Array To Write-Host

It’s straightforward to write down data saved in an array to the host. All that’s required is so as to add the array subsequent to Write-Host.

Right here is an instance…

$Array = @("System", "Enter", "Output")
Write-Host $Array

On this instance, the primary command saves the array in a variable I name Array. Then, within the second command, write-host writes what’s saved within the variable on the host.

Sadly, as you may see from the screenshot above, write-host writes all of the array members in a single line. However what in order for you them in single strains?

We are able to apply a easy trick to realize this.

First, I’ll break down the array members into their particular person members. Listed here are the instructions that do that…

$Array[0]
$Array[2]
$Array[3]

I’ve one other instance later on this information that explains find out how to break array members into particular person objects.

As soon as I’ve damaged the array members, I’ll use introduce the characters `r`n utilized in a earlier instance. Right here is the ultimate script…

$Array = @("System", "Enter", "Output")
Write-Host $Array
$array0 = $Array[0]
$array1 = $Array[1]
$array2 = $Array[2]
Write-Host $array0`r`n$array1`r`n$array2

And right here you’ve got your end result…

How To Show Formatted Output With PowerShell Write-Host

On this instance, I need to show the output of Get-Service on my PowerShell console. So, first, I save the output of Get-Service to the providers variable.

$providers = Get-Service

Then, to make certain that the desk is correctly formatted, I name the variable.

$providers

So, all the pieces appears to be like good till I name the identical variable in a write-host command. See the command beneath this screenshot.

How To Display Formatted Output With PowerShell Write-Host
Write-Host $providers

Right here is how Write-Host shows the knowledge. Clearly, not fascinating!

To repair the issue, use the command beneath:

$providers | Format-Desk | Out-String |% {Write-Host $_}

And now all the pieces is displayed properly as anticipated!

How To Write Present Time With PowerShell Write-Host

The command you utilize to write down present time with Write-Host is determined by the way you need to format your output.

The merely command that writes present date and time to host is…

Write-Host $(Get-Date)

This command shows the present date and time…

Nonetheless, if you wish to show present time with out the date, use the command beneath:

$currentTime = Get-Date -format "HH:mm:ss"
Write-Host "Backups full at $currentTime"

The primary command saves the present time within the currentTime variable. Then, the second command shows write the knowledge within the Write-Host on the console.

How To Write Current Time With PowerShell Write-Host

How To Use Write-Host To Write Texts With Totally different Colours On The Identical Line

The trick to writing texts with totally different colours on the identical line is to make use of two Write-Host instructions in the identical line. Nonetheless, it is advisable embrace the NoNewline parameter within the first Write-Host command.

Then, separate the 2 instructions with semi-colon (;) character.

To exhibit this in an actual command, I’ll write the next texts within the colours specified.

“This colour is pink” – written in pink
“This colour is blue” – written in blue

Right here is the command that does the magic!

Write-Host "This colour is pink" -ForegroundColor pink -NoNewline; Write-Host " This colour is blue" -ForegroundColor blue

Right here is the results of the command in PowerShell. Did you discover that there’s a area between the 2 texts?

How To Use Write-Host To Write Texts With Different Colors On The Same Line

I added that area by including an area after the primary quote within the second Write-Host command – ” This colour is blue”.

Shifting on, in order for you PowerShell to maneuver the second textual content to a brand new line, take away the NoNewline parameter within the first Write-Host command.

The command will now seem like this…

Write-Host "This colour is pink" -ForegroundColor pink; Write-Host " This colour is blue" -ForegroundColor blue

And the end result like this…

How To Use Write-Host To Write Texts With Different Colors On The Same Line

PowerShell Write-Host: Incessantly Requested Questions

PowerShell Write-Host Frequently Asked Questions
1. Ought to I Use Write-Output or Write-Host?

Whether or not you selected to make use of Write-Output or Write-Host is determined by what you need to obtain. When you simply need to write some texts on the console (host) however don’t have to ship the knowledge to the pipeline, use Write-Host.

Then again, in case you want to write a textual content to the console and in addition ship it to the pipeline, it is advisable use Write-Output.

For an in depth rationalization of the distinction between Write-Output andr Write-Host (with examples), go to the How To Use PowerShell Write-Host vs Write-Output (hyperlink opens in a brand new browser tab) part of this information.

2. What Is Write-Host Cmdlet Used For?

The Write-Host cmdlet is used to write down objects to host (show on the PowerShell console). You will need to word that Write-Host doesn’t generate pipeline output.

So, you can not ship the output of Write-Host to a different cmdlet by way of the pipeline. If it is advisable ship a textual content displayed on the console to the pipeline, use Write-Output as an alternative.

3. How Do You Write To A Textual content File In PowerShell?

There are three cmdlets you should use to write down texts to a textual content file in PowerShell. You should use Out-File or Add-Content material.

Along with these two, you may also use Set-Content material to to write down texts to a textual content file in PowerShell. To learn to write texts to a textual content file in PowerShell, learn my article How To Use PowerShell To Write To A Textual content File (opens in a brand new browser tab).

4. How Do I Write To A log File In PowerShell?

You should use three cmdlets to write down to a log file. To write down to a log file, you should use Set-Content material or Add-Content material.

Alternatively, you should use the Out-File cmdlet. You may also write errors to a log file in PowerShell.

To write down errors to a log file, use the Write-Error cmdlet.

5. What Is A Log File In PowerShell?

In PowerShell, a log file is a textual content file with both the .log or .txt file extension. You should use a log file to write down data utilizing Set-Content material, Add-Content material, or Out-File cmdlets.

PowerShell Write-Host: My Ultimate Ideas

PowerShell Write-Host My Final Thoughts

The PowerShell Write-Host is a cmdlet you’ll seemingly want in your day-to-day scripting duties. When you ever have to show data on the PowerShell console, you’ll use this cmdlet.

On this information, I’ve supplied an in depth rationalization of the important issues it is advisable use Write-Host. Particularly, I lined the syntax and the parameters of PowerShell Write-Host.

Not solely that, however I additionally mentioned a number of examples and purposes of the Write-Host cmdlet.

My hope is that I used to be capable of clarify this extraordinarily helpful cmdlet to you. I additionally hope that you simply discovered the information straightforward to observe and straightforward to know.

When you discovered this information straightforward to know and informative, kindly vote Sure to the Was this put up useful? beneath. Then again, in case you choose to share your ideas on this text, kindly use the “LEAVE A REPLY” on the finish of this web page.

Lastly, I strongly consider that you simply’ll discover our different Home windows PowerShell Defined articles helpful as effectively. We even have Home windows PowerShell How-To Guides, that gives you extra methods to be taught PowerShell.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments