{"id":596,"date":"2008-08-12T11:00:52","date_gmt":"2008-08-12T19:00:52","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/2008\/08\/12\/powershell-msgbox\/"},"modified":"2008-08-12T11:12:21","modified_gmt":"2008-08-12T19:12:21","slug":"powershell-msgbox","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2008\/08\/12\/powershell-msgbox\/","title":{"rendered":"PowerShell MsgBox"},"content":{"rendered":"<p>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&#8217;s stick with what we have.\u00a0 If you really need a message box, ala VBScript, then simply call it from PowerShell.\u00a0 All you need to do is use the [microsoft.visualbasic.interaction] class and the MsgBox method.<\/p>\n<p>Before you can use it, you will likely need to load the .NET assembly:<\/p>\n<p><span style=\"font-family: Consolas; color: #0000ff;\">PS C:\\&gt; [reflection.assembly]::loadwithpartialname(&#8220;microsoft.visualbasic&#8221;) | Out-Null <\/span><\/p>\n<p>Once loaded you can call a message box as easy as this:<a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/08\/basicmsgbox.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/08\/basicmsgbox-thumb.png\" border=\"0\" alt=\"basicmsgbox\" width=\"237\" height=\"156\" align=\"right\" \/><\/a><\/p>\n<p><span style=\"font-family: Consolas; color: #0000ff;\">PS C:\\&gt; [microsoft.visualbasic.interaction]::MsgBox(&#8220;Hello everyone&#8221;)<br \/>\nOk<br \/>\nPS C:\\&gt;<\/span><\/p>\n<p>As you can see it is a barebones message box.<\/p>\n<p>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.<\/p>\n<p>Here is a function to create a message box and sample code that demonstrates it.<\/p>\n<div style=\"border: 1px solid gray; margin: 20px 0px 10px; padding: 4px; overflow: auto; font-size: 8pt; width: 97.5%; cursor: text; max-height: 200px; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\">\n<div style=\"border-style: none; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\">\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   1:<\/span> Function Show-Msgbox {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   2:<\/span>   Param([string]$message=$(Throw <span style=\"color: #006080;\">\"You must specify a message\"<\/span>),<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   3:<\/span>       [string]$button=<span style=\"color: #006080;\">\"okonly\"<\/span>,<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   4:<\/span>       [string]$icon=<span style=\"color: #006080;\">\"information\"<\/span>,<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   5:<\/span>       [string]$title=<span style=\"color: #006080;\">\"Message Box\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   6:<\/span>      )<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   7:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">   8:<\/span> <span style=\"color: #008000;\"># Buttons: OkOnly, OkCancel, AbortRetryIgnore, YesNoCancel, YesNo, RetryCancel<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">   9:<\/span> <span style=\"color: #008000;\"># Icons: Critical, Question, Exclamation, Information<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  10:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  11:<\/span>   [reflection.assembly]::loadwithpartialname(<span style=\"color: #006080;\">\"microsoft.visualbasic\"<\/span>) | Out-Null<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  12:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  13:<\/span>   [microsoft.visualbasic.interaction]::Msgbox($message,<span style=\"color: #006080;\">\"$button,$icon\"<\/span>,$title)<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  14:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  15:<\/span>  }<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  16:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  17:<\/span> $rc=Show-Msgbox -message <span style=\"color: #006080;\">\"Do you know what you're doing?\"<\/span> `<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  18:<\/span> -icon <span style=\"color: #006080;\">\"exclamation\"<\/span> -button <span style=\"color: #006080;\">\"YesNoCancel\"<\/span> -title <span style=\"color: #006080;\">\"Hey $env:username!!\"<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  19:<\/span><\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  20:<\/span> Switch ($rc) {<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  21:<\/span>  <span style=\"color: #006080;\">\"Yes\"<\/span> {<span style=\"color: #006080;\">\"I hope your resume is up to date.\"<\/span>}<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  22:<\/span>  <span style=\"color: #006080;\">\"No\"<\/span> {<span style=\"color: #006080;\">\"Wise move.\"<\/span>}<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  23:<\/span>  <span style=\"color: #006080;\">\"cancel\"<\/span> {<span style=\"color: #006080;\">\"When in doubt, punt.\"<\/span>}<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: #f4f4f4;\"><span style=\"color: #606060;\">  24:<\/span> }<\/pre>\n<pre style=\"border-style: none; margin: 0em; padding: 0px; overflow: visible; font-size: 8pt; width: 100%; color: black; line-height: 12pt; font-family: consolas,'Courier New',courier,monospace; background-color: white;\"><span style=\"color: #606060;\">  25:<\/span><\/pre>\n<\/div>\n<\/div>\n<p>If you execute this you should expect a result like this:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/08\/pshmsgbox.png\"><img loading=\"lazy\" decoding=\"async\" style=\"border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/08\/pshmsgbox-thumb.png\" border=\"0\" alt=\"pshmsgbox\" width=\"353\" height=\"173\" \/><\/a><\/p>\n<p>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:<\/p>\n<p><span style=\"font-family: Consolas; color: #0000ff;\">$rc=Show-Msgbox -message &#8220;Do you know what you&#8217;re doing?&#8221; `<br \/>\n-icon &#8220;exclamation&#8221; -button &#8220;YesNoCancel&#8221; -title &#8220;Hey $env:username!!&#8221; <\/span><\/p>\n<p><span style=\"font-family: Consolas; color: #0000ff;\">Switch ($rc) {<br \/>\n&#8220;Yes&#8221; {&#8220;I hope your resume is up to date.&#8221;}<br \/>\n&#8220;No&#8221; {&#8220;Wise move.&#8221;}<br \/>\n&#8220;cancel&#8221; {&#8220;When in doubt, punt.&#8221;}<br \/>\n}<\/span><\/p>\n<p>You&#8217;re probably thinking, &#8220;This is great but where&#8217;s my Inputbox?&#8221; Don&#8217;t worry.\u00a0 It&#8217;s coming assuming you don&#8217;t figure it out on your own.<\/p>\n<div id=\"scid:fb3a1972-4489-4e52-abe7-25a00bb07fdf:6774b1d5-a087-49e9-b9e6-d10f4156e204\" class=\"wlWriterSmartContent\" style=\"padding-right: 0px; display: inline; padding-left: 0px; padding-bottom: 0px; margin: 0px; padding-top: 0px\">\n<p>You can download this sample function and script <a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2008\/08\/show-msgbox.txt\" target=\"_blank\">here<\/a><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;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.<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[24,25],"tags":[198,268,28,269,996],"class_list":["post-596","post","type-post","status-publish","format-standard","hentry","category-vbscript","category-windows-powershell","tag-function","tag-msgbox","tag-powershell","tag-switch","tag-vbscript"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/596","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=596"}],"version-history":[{"count":2,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/596\/revisions"}],"predecessor-version":[{"id":597,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/596\/revisions\/597"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=596"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=596"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=596"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}