The PowerShell Write-Host cmdlet is used to output textual content to the console for the person. It’s generally used to indicate the progress of a script, output outcomes, or present directions for the person.
On this article, we’re going to check out find out how to use the Write-Host cmdlet, the completely different choices that it comes with, and I’ll present you a few examples.
PowerShell Write-Host CmdLet
The Write-Host
cmdlet is definitely a wrapper for the older Write-Data cmdlet. Till PowerShell 5.0 we had to make use of the Write-Data
or Write-Output
cmdlet to output data to the console. The outdated cmdlets didn’t include a number of options, whereas write-host offers us a few styling choices that we will use:
Parameter | Description |
---|---|
-ForegroundColor | Shade of the textual content |
-BackgroundColor | Background coloration of the textual content |
-NoNewline | Received’t add a newline after the output |
-Separator | Provides a separator between the output of every merchandise in an objects or array |
By default, the Write-Host
cmdlet will output a string on a brand new line within the console, utilizing the default coloration of the console (more often than not white):
Write-Host "Welcome to the Lazy Script!"
Utilizing Colours in Write-Host
Colours are an effective way to draw the person’s consideration or inform one thing in regards to the standing non-verbally. For instance, yellow textual content typically signifies a warning whereas purple textual content is usually doubtless an error. In PowerShell we will assign a coloration for the textual content, utilizing the -foregroundcolor
parameter and a background coloration for the textual content with -backgroundcolor
.
We are able to use the next colours for each parameters:
- Black
- DarkBlue
- DarkGreen
- DarkCyan
- DarkRed
- DarkMagenta
- DarkYellow
- Grey
- DarkGray
- Blue
- Inexperienced
- Cyan
- Pink
- Magenta
- Yellow
- White
Formatting the Output
By default, every string that’s outputted by Write-Host is displayed on a brand new line. Generally that is wonderful, however generally you don’t need a new line, or perhaps an additional new line between the outputs.
Let’s first check out find out how to append the outcomes behind the earlier Write-Host output. To do that we will use the -NoNewline
parameter. When added, the subsequent output will append to the prevailing line:
Write-Host "Variety of mailboxes: " -NoNewline # Get all mailboxes operate $mailboxes = Get-AllMailboxes Write-Host $mailboxes # Outcome: Variety of mailboxes: 5
As you’ll be able to see, the worth $mailboxes
is appended behind the road “Variety of mailboxes”.
If you wish to create an additional new clean line between the outputs, then we will use the `n
character. It will add an additional new line behind the output of the write-host
cmdlet:
Write-Host "Getting all person mailboxes `n" -ForegroundColor Cyan Write-Host "Shared mailboxes discovered `n `n" -ForegroundColor Yellow Write-Host "Script completed" -ForegroundColor Inexperienced
Utilizing Variables inside Write Host
Good to know is that we will use variables contained in the output of the write-host cmdlet. A easy string variable will be included within the string of write-host when utilizing the double-quotes " "
. Needless to say you’ll be able to’t use variables inside strings wrapped with single-quotes ' '
. The distinction right here is that PowerShell solely processes strings which might be wrapped in double quotes.
$person = "[email protected]" Write-Host "Getting mailbox of $person" # Outcome Getting mailbox of [email protected]
However when working with objects, you might need seen that you could’t output the gadgets of an object. For instance, this received’t work:
$person = [PSCustomObject]@{ DisplayName = "Megan" E mail = "[email protected]" } Write-Host "Getting mailbox of $person.displayname"
To indicate the show title of the person object, we might want to wrap it in parentheses, like this:
$person = [PSCustomObject]@{ DisplayName = "Megan" E mail = "[email protected]" } Write-Host "Getting mailbox of $($person.DisplayName)" # Outcome Getting mailbox of Megan
Utilizing a Separator
If you wish to output the outcomes of an array to the console, then the outcomes will probably be displayed on a single row by default. For instance, I’ve a CSV file with some fruits. After we import the CSV file and output the outcome then will probably be formatted like this:
$instance = import-csv -path C:tempexample.csv -Header Fruit Write-Host $instance.fruit # Outcome Apple Banana Pear Kiwi Raspberry Melon
To indicate every fruit by itself line or separate every fruit with a personality, we will use the -Separator
parameter:
# Present outcomes on a brand new line $instance = import-csv -path C:tempexample.csv -Header Fruit Write-Host $instance.fruit -Separator "`n" # Outcome Apple Banana Pear Kiwi Raspberry Melon # Separate outcomes with an area and sprint $instance = import-csv -path C:tempexample.csv -Header Fruit Write-Host $instance.fruit -Separator " - " # Outcome Apple - Banana - Pear - Kiwi - Raspberry - Melon
Write Host Formatting Examples
With some creativity, you’ll be able to create completely different banners, or alerts in your scripts that actually stand out. Under I collect a few examples as inspirations for you. You probably have different good examples, please share them within the feedback under!
Including a brand new line above and under a warning message, including some house across the message, and utilizing a background coloration can actually make a message stand out:
Write-Host "`n" Write-Host " RUNNING IN TEST MODE " -BackgroundColor Yellow -ForegroundColor Black Write-Host "`n"
Script banners are at all times a little bit of a puzzle to get proper. Within the code instance under it doesn’t look aligned, however when utilizing it in a script you will notice an ideal banner.
Write-Host "`n" Write-Host " ------------------------------------------------- " -ForegroundColor Cyan Write-Host " | | " -ForegroundColor Cyan Write-Host " | Create new AD Person | " -ForegroundColor Cyan Write-Host " | Model 1.7 | " -ForegroundColor Cyan Write-Host " | | " -ForegroundColor Cyan Write-Host " | Writer R. Mens - LazyAdmin.nl | " -ForegroundColor Cyan Write-Host " | | " -ForegroundColor Cyan Write-Host " ------------------------------------------------- " -ForegroundColor Cyan Write-Host "`n"
We are able to additionally do one thing enjoyable with the foreground colours and a few ASCII artwork. The code under divides the place of every character with 6. Relying on the outcome, it should assign a coloration to the character. Through the use of the -NoNewline
parameter we will append the outcomes:
$textual content = " _ ___ _ _ | | / _ | | (_) | | __ _ _____ _/ /_ __| |_ __ ___ _ _ __ | | / _ |_ / | | | _ |/ _ | '_ _ | | '_ | |___| (_| |/ /| |_| | | | | (_| | | | | | | | | | | _____/__,_/___|__, _| |_/__,_|_| |_| |_|_|_| |_| __/ | |___/ " for ($i=0; $i -lt $textual content.size; $i++) { change ($i % 6) { 0 { $c = "white" } 2 { $c = "inexperienced" } 4 { $c = "blue" } default { $c = "cyan" } } write-host $textual content[$i] -NoNewline -ForegroundColor $c }
Wrapping Up
The Write-Host cmdlet is an effective way to maintain the person of your script knowledgeable in regards to the progress of your scripts. It may give directions to the person, present the outcomes or just inform the person when the script is accomplished or bumped into an error.
Make good use of the foreground colours, as a result of they actually assist with shortly figuring out the which means of a message.
I hope you want this text, when you have any questions, simply drop a remark under!