Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



Windows PowerShell Write-Host

Windows PowerShell Write-Host

PowerShell's Write-Host is similar to the WScript.Echo command.  But as usual PowerShell's commands are just that bit easier to understand and achieve a little bit more than VBScript.

Windows PowerShell Write-Host Topics

 ♣

PowerShell Pre-requisites and Checklist

In the case of Windows 7 and Server 2008, you don't need to download any extra files, just 'Add Feature' Windows PowerShell.  However, for older operating systems, installing can be confusing because there are different versions of PowerShell for XP, Windows Server 2003 and Vista.  For such legacy systems only, you need to download PowerShell from Microsoft's site.

Once you have installed PowerShell 2.0, I recommend choosing the ISE (Integrated Scripting Engine) version, it will save you buying a text editor.

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: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft 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.

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
$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:
Get-Date -Uformat "%A %d-%m-%Y"

Further Research on Write-Host

PowerShell Write-Host Parameters

# Extra parameters for PowerShell's Write-Host
Clear-host
Get-Help Write-Host -full

Two parameters, which most script writers use sooner or later are -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)Review of Orion NPM v10

Solarwinds' Orion 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.

Perhaps the NPM's best feature is the way it suggests solutions to network problems.  Its second best feature is 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 take advantage of Solarwinds' offer.

Download a free trial of the Network Performance Monitor.

Researching Similar PowerShell Cmdlets

# PowerShell CSV Cmdlet Research
Clear-Host
Get-Command -noun Host

Out-Host looks interesting, there again researching PowerShell cmdlets with -noun or -verb always throws up at least one surprise.

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 keep experimenting with Write-Host until you find the particular output your task requires.

If you like this page then please share it with your friends

 


See more Microsoft PowerShell output tutorials:

PShell Home  • Out-File  • Out-GridView  • -f format  • Files  • Pipe to file

Export-CSV  • Import-CSV  • Format-Table  • Format-List  • Read-Host  • Write-Host

ConvertTo-Html   • Windows PowerShell   • PowerShell 3.0 Redirection

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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  Take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.