LEARN with scripting TRAINING CREATE with SCRIPTING TOOLS SHARE in a COMMUNITY BUY Scripting Products

SAPIEN Solutions

SAPIEN homepage
SAPIEN Software
PrimalScript script editor+ide
PrimalScope script debugger
Free Tools script utilities
SAPIEN Press tech books
ScriptingAnswers.com learn+share
ScriptingOutpost.com online store
Blog.Sapien.com official blog
Contact Us

 

PowerShell MsgBox

If you came from the VBScript world you may be missing a few old friends in PowerShell. Because Powershell is a management console, it lacks a GUI unless you create one using Windows forms. Things will change a bit in PowerShell v2.0 but for now let’s stick with what we have.  If you really need a message box, ala VBScript, then simply call it from PowerShell.  All you need to do is use the [microsoft.visualbasic.interaction] class and the MsgBox method.

Before you can use it, you will likely need to load the .NET assembly:

PS C:\> [reflection.assembly]::loadwithpartialname(”microsoft.visualbasic”) | Out-Null

Once loaded you can call a message box as easy as this:basicmsgbox

PS C:\> [microsoft.visualbasic.interaction]::MsgBox(”Hello everyone”)
Ok
PS C:\>

As you can see it is a barebones message box.

The MsgBox method supports all the things you remember from VBScript like different buttons, a title and icons. Valid button choices include: OkOnly, OkCancel, AbortRetryIgnore, YesNoCancel, YesNo, RetryCancel. There are a few other options but these are the ones you are most likely to use. Valid icons include: Critical, Question, Exclamation, Information.

Here is a function to create a message box and sample code that demonstrates it.

   1: Function Show-Msgbox {
   2:   Param([string]$message=$(Throw "You must specify a message"),
   3:       [string]$button="okonly",
   4:       [string]$icon="information",
   5:       [string]$title="Message Box"
   6:      )
   7:
   8: # Buttons: OkOnly, OkCancel, AbortRetryIgnore, YesNoCancel, YesNo, RetryCancel
   9: # Icons: Critical, Question, Exclamation, Information
  10:
  11:   [reflection.assembly]::loadwithpartialname("microsoft.visualbasic") | Out-Null
  12:
  13:   [microsoft.visualbasic.interaction]::Msgbox($message,"$button,$icon",$title)
  14:
  15:  }
  16:
  17: $rc=Show-Msgbox -message "Do you know what you're doing?" `
  18: -icon "exclamation" -button "YesNoCancel" -title "Hey $env:username!!"
  19:
  20: Switch ($rc) {
  21:  "Yes" {"I hope your resume is up to date."}
  22:  "No" {"Wise move."}
  23:  "cancel" {"When in doubt, punt."}
  24: }
  25:

If you execute this you should expect a result like this:

pshmsgbox

The MsgBox method will write the name of the clicked button to the pipeline. This allows me to save the MsgBox results to a variable and use a Switch construct to take action based on the value:

$rc=Show-Msgbox -message “Do you know what you’re doing?” `
-icon “exclamation” -button “YesNoCancel” -title “Hey $env:username!!”

Switch ($rc) {
“Yes” {”I hope your resume is up to date.”}
“No” {”Wise move.”}
“cancel” {”When in doubt, punt.”}
}

You’re probably thinking, “This is great but where’s my Inputbox?” Don’t worry.  It’s coming assuming you don’t figure it out on your own.

You can download this sample function and script here

[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Technorati Tags: , , , ,

Tags: , , , ,

2 Responses to “PowerShell MsgBox”

  1. Josh G Says:

    I like the tips, but coming from a vbscript world, I would highly recommend learning winForms. They are nuts and you can do a lot more with them. If you nail winForms, it will blow your mind. You will never want to use MSGBOX again.. Trust me from experience

  2. Jeffery Hicks Says:

    If you have some good examples, please share as WinForms take some time to develop.

Leave a Reply


Entries (RSS) and Comments (RSS).