Converting the Windows Script Host Popup Method

Definition: Displays text in a pop-up message box.

OK, we admit it: there’s no elegant way to create a pop-up message box using Windows PowerShell. PowerShell has no built-in mechanism for creating message boxes, and while the .NET Framework does have such a mechanism, .NET message boxes called from PowerShell end up being displayed behind the console window, right where no one can see them. So what do you do if you want to display a message in a pop-up message box? We suggest you go ahead and use Windows Script Host and the Popup method.

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",5,"Test Message Box",1)

Incidentally, the second parameter (the 5) tells PowerShell to display the message box for a maximum of 5 seconds. If the user hasn’t dismissed the message box by the time those 5 seconds have expired then the message box will automatically disappear and the script will resume. In that case, $b will be set to -1, which indicates that the message box timed out without the user clicking a button.

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