Scripting Tips and Tricks: Message Boxes


I used to write a lot of VBScript scripts* and a high percentage of them employed a dialogue box or two. Whether using the WScript.Shell PopUp method or the MsgBox keyword, I delighted in writing messages to screen!

 

 

How times have changed: I can't recall ever using a message box in a PowerShell script; on occasion, I use Write-Host and I certainly make use of Write-Verbose, but, sadly, no dialogue boxes…

So why a post about message boxes, you ask? Well, I was recently asked the following question…

 

How do you generate a message box with PowerShell?

Well, two techniques sprang to mind:

1 – Using the aforementioned PopUp method of the WScript.Shell COM object…

(New-Object -COM Wscript.Shell).PopUp("Business Technology would like to reboot this computer!`n`n", 0, "Business Technology Software Update", 49)

 

NB – In the above example 'OK' returns 1 and 'Cancel' returns 2.

 

2 – Using the Show method of the .Net MessageBox class (preferred)…

[System.Windows.Forms.MessageBox]::Show("Business Technology would like to reboot this computer!`n`n", "Business Technology Software Update", "OKCancel", "Warning" )

 

NB – In this example 'OK' returns, er, OK and 'Cancel' returns Cancel.

 

I wonder why I no longer use message boxes? Perhaps it's due to the type of scripts I now write or were they just a passing obsession? Whatever the answer, I might shoehorn one into my next script… just for old times' sake.

 

*Is 'VBScript scripts' a pleonasm? Answers on a postcard (or use the comments feature)…

 

Comments (1)

Skip to main content