Converting the Windows Script Host Echo Method

Definition: Outputs text to either a message box or the command console window.


In Windows Script Host messages echoed back to the screen will appear in one of two locations, depending upon the script host your script was started under: when running under Cscript messages are echoed back to the console window; when running under Wscript, messages are echoed back in a message box.

In Windows PowerShell messages are only echoed back to the console window; that’s truly regardless of whether you echo back the messages using the Write-Host cmdlet or if you simply specify the values to be echoed. For example, both of these commands echo the message This is a test to the command window:

Write-Host "This is a test"
"This is a test"

For those of you who grew up writing batch files the PowerShell alias echo also echoes information to the console window:

echo "This is a test"

But what if you want to echo messages back using a message box? Well, to be honest, that’s a bit of a problem: PowerShell includes no built-in way to display information in a message box. In theory, you can use the .NET Framework to display messages in a message box; unfortunately, though, those message boxes inevitably appear behind the console window, effectively making them invisible to the user. The .NET Framework is not the answer to this problem.

So what is the answer to this problem? Well, believe it or not, the easiest way to get PowerShell to display information in a message box is to use Windows Script Host and the WSH Popup method. For example, the following command uses New-Object (and the –comobject parameter) to create an instance of the Wscript.Shell object. Once the Shell object has been created, the WSH Popup method is used to display a message box. In turn, the resulting action (that is, the value of the button the user clicked to dismiss the message box) will be stored in the variable $b:

$a = new-object -comobject
$b = $a.popup("This is a test",0,"Test Message Box",1)

See conversions of other Windows Script Host methods and properties.
Return to the VBScript to Windows PowerShell home page