Windows PowerShell Write-Host
PowerShell’s Write-Host is similar to the VBScript command WScript.Echo command.
My advice is only use this command if you have a compelling reason to see stuff displayed on a monitor. I find that the underlying commands work fine without preceding them with Write-Host. Furthermore, Write-Host occasionally causes a perfectly sound command to fail.
Windows PowerShell Write-Host Topics
- Example 1: PowerShell Write-Host
- Example 2: Write-Host With Single or Double Quotes
- PowerShell Color Parameters
- Why You Don’t Need Write-Host 95% of the Time
♣
Example 1: PowerShell Write-Host
I have decided to swerve ‘Hello World’ examples and instead set Write-Host to a real life task of reporting information about your network card’s Ip.
# PowerShell Write-Host Example
Clear-Host
$strComputer = "."
$colItems = Get-Wmiobject "Win32_NetworkAdapterConfiguration" `
-computername $strComputer | Where{$_.IpEnabled -Match "True"}
foreach ($objItem in $colItems) {
Write-Host "MACAddress : " $objItem.MACAddress
Write-Host "IPAddress : " $objItem.IPAddress
Write-Host "IPEnabled : " $objItem.IPEnabled
Write-Host "DNS Servers : " $objItem.DNSServerSearchOrder
}
Note 1: Observe how there is no extra punctuation between "The Literal " (MacAddress 🙂 and the $variable ($objItem.MACAddress). For that reason I left a space just before the closing speech mark.
Challenge: Just to test your own understanding, experiment with changing "MACAddress : " to Plain "MAC ".
Guy Recommends: Free WMI Monitor for PowerShell
Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems. Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.
Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.
Download your free copy of WMI Monitor
Example 2: Write-Host With Single or Double Quotes
Plain Write-Host really is straight-forward to use. One of the few tricky scenarios is when you try to be flashy and combine literals and $variables through the use of quotes.
# PowerShell Write Host Single Quotes
$Now = Get-Date
Write-Host ‘Today is $Now -enjoy! ‘
Note 2: Write-Host interprets a single quote as a literal, sadly it ignores $Now and the underlying Get-Date.
# PowerShell Write Host Double Quotes
Clear-Host
$Now = Get-Date
Write-Host "Today is $Now -enjoy!"
Note 3: This result is probably what the designer of the above script intended. If you run this example then you see today’s date surrounded seamlessly by a few words.
Challenge: For UK readers try swapping this code:
$Now = Get-Date -Uformat "%A %d-%b-%Y"
Further Research on Write-Host
PowerShell Write-Host Parameters
# Extra Parameters for PowerShell’s Write-Host
Clear-host
Get-Help Write-Host -full
Help reveals two parameters, which most script writers use sooner or later, namely -BackGroundColor and -ForeGroundColor.
PowerShell Color Parameters
Take care with Write-Host ForeGroundColor and BackGroundColor as you can produce some truly hideous combinations thus!
# PowerShell Color Parameters
$Date = Get-Date
Write-Host $Date –ForeGroundColor green -b magenta
Write-Host $Date -fore yellow -back green
Write-Host $Date -f red –BackGroundColor darkblue
Apart from black and white, other Write-Host colors include, gray, blue, red and yellow.
Guy Recommends: A Free Trial of the Network Performance Monitor (NPM) v11.5
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance Monitor
Why You Don’t Need Write-Host 95% of the Time
Let us return to earlier examples, and just omit Write-Host altogether
# Why You Don’t Need Write-Host 95% of the Time
Clear-Host
$Now = Get-Date
Write-Host "Today is $Now -enjoy! (Write-Host)"
"Today is $Now -enjoy! "
Note 4: The main command works fine even on screen, Write-Host adds nothing to the output.
Where Write-Host Gets in the Way
Clear-Host
$Xmas = ([System.Datetime]"December 25 2013") -(Get-date)
Write-Host "Santa in " + [Math]::Truncate(($Xmas.Days)/7) + " weeks"
"Santa in " + [Math]::Truncate(($Xmas.Days)/7) + " weeks"
See How Write-Host Creates the Problem
Santa in + [Math]::Truncate 49.4285714285714 + weeks
Santa in 49 weeks
In this output Write-Host produces an undesirable effect. While you can overcome the problem by enclosing the [Math]… in parentheses, it’s simpler to omit the cmdlet altogether.
Researching Similar PowerShell Cmdlets
-Verb Write
# PowerShell Write Cmdlet Research
Get-Command -Verb Write
Name
——————
Write-Debug
Write-Error
Write-EventLog
Write-Host
Write-Output
Write-Progress
Write-Verbose
Write-Warning
-Noun Host
# PowerShell Host Cmdlet Research
Get-Command -Noun Host
Researching PowerShell cmdlets with -Noun or -Verb always throws up at least one surprise, in this instance Out-Host looks interesting, and often use Clear-Host to produce a blank slate before I run another script.
See another 'Write' cmdlet Eventlog »
Summary of PowerShell Write-Host
With PowerShell’s Write-Host what you see is what you get on screen. It’s an easy cmdlet to understand, especially if you are familiar echo commands from other scripting languages. My advice is to only use Write-Host if absolutely necessary.
If you like this page then please share it with your friends
See more Microsoft PowerShell output tutorials:
• PShell Home • Out-File • Out-GridView • ConvertTo-Csv • ConvertTo-Html • ConvertFrom-Csv
• Tee-Object • Import-CSV • Format-Table • PowerShell Here-String • ConvertFrom-JSON
• Export-CliXml • Format-List • Read-Host • PowerShell Get-History • -f format • Pipe to file
Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.