Friday Fun: Messagebox Writer

yellowsub For today’s fun I’m revisiting a topic I’m sure I’ve written about in the past and that is creating a graphical message box in PowerShell. There are a few ways you can do this from using the Visual Basic classes, the old standard WScript.Shell object from VBScript or using WinForms. They all basically work the same way and you probably can’t tell from the message box what was used to create it. So I took my version of New-MessageBox which uses the Visual Basic interaction class and updated it a bit.

Here’s the new function.

This version has been tweaked to include better parameter validation. I also modified it so that the parameters accept pipeline input by property name. I think the function is pretty well documented so I won’t spend much time discussing it. You can try it out for yourself. Be sure to test with bad parameter values so you can see how validation works.

If you had grabbed an older version of this function, there is one breaking change. In the past I think I had the function always write the clicked button value to the pipeline. Now the function doesn’t write anything to the pipeline unless you use -Passthru. When you run a command like this:

You will get this:

MsgBoxYesNo

Nothing will be written to the pipeline because I didn’t use -Passthru. Now for the extra-fun part. If you read through the code listing, you may have seen references to a speech assembly. I figured, why not include an option to have the message spoken aloud? So I’m using the System.Speech.Synthesis.SpeechSynthesizer class to speak the text in the message box. Use the -Voice parameter to turn this on. PowerShell will use the default voice for your system. But wait, there’s more! I also included a second parameter, VoiceGender, so that you can specify if you want a Male or Female voice. On later client operating systems I believe you get one of each automatically.

Click here to view the video full size or right-click and save the video clip.

Here’s the sample script I ran.

Enjoy!