Converting VBScript's MsgBox Function

Definition: Displays a message in a dialog box, waits for the user to click a button, and returns a value indicating which button the user clicked.


Windows PowerShell does not posses built-in methods for displaying message boxes. However, it’s very easy to use the New-Object Cmdlet to create an instance of the Windows Script Host (WSH) Shell object; from there you can use the WSH Popup method to display a message box. The following example first uses New-Object (and the -comobject parameter) to create an instance of the Wscript.Shell object. In the second command, the WSH Popup method is used to display a message box, with the resulting action (that is, the value of the button the user clicked to dismiss the message box) stored in the variable $b:

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

For more information on the parameters used when displaying the message box see the Windows Script Host Language Reference.

Return to the VBScript to Windows PowerShell home page