{"id":990,"date":"2013-07-23T17:17:57","date_gmt":"2013-07-23T15:17:57","guid":{"rendered":"http:\/\/michlstechblog.info\/blog\/?p=990"},"modified":"2013-07-23T17:19:22","modified_gmt":"2013-07-23T15:19:22","slug":"powershell-show-a-messagebox","status":"publish","type":"post","link":"https:\/\/michlstechblog.info\/blog\/powershell-show-a-messagebox\/","title":{"rendered":"Powershell: How to show a message box"},"content":{"rendered":"<div class=\"twoclick_social_bookmarks_post_990 social_share_privacy clearfix 1.6.4 locale-en_US sprite-en_US\"><\/div><div class=\"twoclick-js\"><script type=\"text\/javascript\">\/* <![CDATA[ *\/\njQuery(document).ready(function($){if($('.twoclick_social_bookmarks_post_990')){$('.twoclick_social_bookmarks_post_990').socialSharePrivacy({\"services\":{\"flattr\":{\"uid\":\"Michl\",\"status\":\"on\",\"the_title\":\"Powershell%3A%20How%20to%20show%20a%20message%20box\",\"the_excerpt\":\"Sometimes%20while%20a%20powershell%20script%20is%20running%20you%20want%20to%20show%20a%20MessageBox%20with%20a%20information%20or%20warning%20to%20the%20user.%20In%20Windows%20Powershell%20no%20Commandlet%20exists%20to%20show%20a%20Message%20Box.%0D%0A%0D%0ANevertheless%20it%20is%20possible%20by%20using%20the%20.NET%20Windows.Forms.MessageBox%20class%3A-%29.%0D%0A%0D%0AFirst%20of%20all%20load%20the%20assembly.%0D%0A%0D%0A%5Bpowershell%5D%0D%0A%23%20Load%20assembly%0D%0A%5BSystem.Reflection.Assembly%5D%3A%3ALoadWithPartialName%28%26quot%3BS%20...\",\"txt_info\":\"2 clicks for more data protection:\\r\\n\\r\\nOnly when you click here, the button will be come active and you can send your recommendation to Flattr. When activating, data are transmitted to third parties. \",\"perma_option\":\"off\"}},\"txt_help\":\"When you activate these fields by clicking, information to Flattr may be transferred abroad, and probably may also stored there.\",\"settings_perma\":\"Enable permanently and accept data transmission. \",\"info_link\":\"http:\\\/\\\/www.heise.de\\\/ct\\\/artikel\\\/2-Klicks-fuer-mehr-Datenschutz-1333879.html\",\"uri\":\"https:\\\/\\\/michlstechblog.info\\\/blog\\\/powershell-show-a-messagebox\\\/\",\"post_id\":990,\"post_title_referrer_track\":\"Powershell%3A+How+to+show+a+message+box\",\"display_infobox\":\"on\"});}});\n\/* ]]> *\/<\/script><\/div><p>Sometimes while a powershell script is running you want to show a MessageBox with a information or warning to the user. In Windows Powershell no Commandlet exists to show a Message Box.<\/p>\n<p>Nevertheless it is possible by using the .NET Windows.Forms.MessageBox class:-).<\/p>\n<p>First of all load the assembly.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n# Load assembly\r\n&#x5B;System.Reflection.Assembly]::LoadWithPartialName(&quot;System.Windows.Forms&quot;)\r\n\r\n<\/pre>\n<p>To show the messagebox call the static function show(&#8220;Message Text&#8221;)<br \/>\n<!--more--><\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$oReturn=&#x5B;System.Windows.Forms.Messagebox]::Show(&quot;This is the Message text&quot;)\r\n\r\n<\/pre>\n<p>The function returned a value of enum System.Windows.Forms.DialogResult, that indicates which Button was pressed.<br \/>\nPossible Returncodes, depending on which button was pressed, are:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n&#x5B;system.enum]::getValues($oReturn.GetType())\r\nNone\r\nOK\r\nCancel\r\nAbort\r\nRetry\r\nIgnore\r\nYes\r\nNo\r\n<\/pre>\n<p>The default Button is the OK Button, there are further 5 combinations, see below.<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n&#x5B;system.enum]::getNames(&#x5B;System.Windows.Forms.MessageBoxButtons])|foreach{&#x5B;console]::Writeline(&quot;{0,20} {1,-40:D}&quot;,$_,&#x5B;System.Windows.Forms.MessageBoxButtons]::$_.value__)}\r\n                  OK 0\r\n            OKCancel 1\r\n    AbortRetryIgnore 2\r\n         YesNoCancel 3\r\n               YesNo 4\r\n         RetryCancel 5\r\n<\/pre>\n<p>An Example, a Message box with an Ok and a Cancel button and a check which button was pressed:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n$oReturn=&#x5B;System.Windows.Forms.MessageBox]::Show(&quot;Message Text&quot;,&quot;Title&quot;,&#x5B;System.Windows.Forms.MessageBoxButtons]::OKCancel)\t\r\nswitch ($oReturn){\r\n\t&quot;OK&quot; {\r\n\t\twrite-host &quot;You pressed OK&quot;\r\n\t\t# Enter some code\r\n\t} \r\n\t&quot;Cancel&quot; {\r\n\t\twrite-host &quot;You pressed Cancel&quot;\r\n\t\t# Enter some code\r\n\t} \r\n}\r\n\r\n<\/pre>\n<p><figure id=\"attachment_1015\" aria-describedby=\"caption-attachment-1015\" style=\"width: 356px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Button_OKChancel.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1015\" alt=\"[System.Windows.Forms.MessageBoxButtons]::OKCancel\" src=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Button_OKChancel.png\" width=\"356\" height=\"142\" srcset=\"https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Button_OKChancel.png 356w, https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Button_OKChancel-300x119.png 300w\" sizes=\"auto, (max-width: 356px) 100vw, 356px\" \/><\/a><figcaption id=\"caption-attachment-1015\" class=\"wp-caption-text\">[System.Windows.Forms.MessageBoxButtons]::OKCancel<\/figcaption><\/figure>Some examples<\/p>\n<p><figure id=\"attachment_1016\" aria-describedby=\"caption-attachment-1016\" style=\"width: 394px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Button_AbortRetryIgnorel.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1016\" alt=\"[System.Windows.Forms.MessageBoxButtons]::AbortRetryIgnore\" src=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Button_AbortRetryIgnorel.png\" width=\"394\" height=\"141\" srcset=\"https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Button_AbortRetryIgnorel.png 394w, https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Button_AbortRetryIgnorel-300x107.png 300w\" sizes=\"auto, (max-width: 394px) 100vw, 394px\" \/><\/a><figcaption id=\"caption-attachment-1016\" class=\"wp-caption-text\">[System.Windows.Forms.MessageBoxButtons]::AbortRetryIgnore<\/figcaption><\/figure><figure id=\"attachment_1012\" aria-describedby=\"caption-attachment-1012\" style=\"width: 373px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Button_YesNoCancell.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1012\" alt=\"[System.Windows.Forms.MessageBoxButtons]::YesNoCancel\" src=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Button_YesNoCancell.png\" width=\"373\" height=\"139\" srcset=\"https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Button_YesNoCancell.png 373w, https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Button_YesNoCancell-300x111.png 300w\" sizes=\"auto, (max-width: 373px) 100vw, 373px\" \/><\/a><figcaption id=\"caption-attachment-1012\" class=\"wp-caption-text\">[System.Windows.Forms.MessageBoxButtons]::YesNoCancel<\/figcaption><\/figure>you can also use the number instead of the numeric constants to specify the buttons<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n&#x5B;System.Windows.Forms.MessageBox]::Show(&quot;Message Text&quot;,&quot;Title&quot;,1)\r\n\r\n<\/pre>\n<p>This oneliner shows all possible Button combinations consecutively<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n&#x5B;system.enum]::getValues(&#x5B;System.Windows.Forms.MessageBoxButtons])|foreach {&#x5B;System.Windows.Forms.MessageBox]::Show(&quot;&#x5B;&quot;+$_.GetType()+&quot;]::&quot;+$_.ToString(),&quot;Message box Buttons&quot;,$_)}\r\n\r\n<\/pre>\n<p>You can style the message box with an icon, 4 are available<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n&#x5B;system.enum]::getNames(&#x5B;System.Windows.Forms.MessageBoxIcon])|foreach{&#x5B;console]::Writeline(&quot;{0,20} {1,-40:D}&quot;,$_,&#x5B;System.Windows.Forms.MessageBoxIcon]::$_.value__)}\r\n                None 0\r\n                Hand 16\r\n               Error 16\r\n                Stop 16\r\n            Question 32\r\n         Exclamation 48\r\n             Warning 48\r\n            Asterisk 64\r\n         Information 64\r\n\r\n<\/pre>\n<p>Example:<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n&#x5B;System.Windows.Forms.MessageBox]::Show(&quot;Message Text&quot;,&quot;Title&quot;,&#x5B;System.Windows.Forms.MessageBoxButtons]::OKCancel,&#x5B;System.Windows.Forms.MessageBoxIcon]::Warning)\r\n\r\n<\/pre>\n<p><figure id=\"attachment_1024\" aria-describedby=\"caption-attachment-1024\" style=\"width: 383px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Warning.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1024\" alt=\"[System.Windows.Forms.MessageBoxIcon]::Warning\" src=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Warning.png\" width=\"383\" height=\"159\" srcset=\"https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Warning.png 383w, https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Warning-300x124.png 300w\" sizes=\"auto, (max-width: 383px) 100vw, 383px\" \/><\/a><figcaption id=\"caption-attachment-1024\" class=\"wp-caption-text\">[System.Windows.Forms.MessageBoxIcon]::Warning<\/figcaption><\/figure>Same with numbers instead of numeric constants<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n&#x5B;System.Windows.Forms.MessageBox]::Show(&quot;Message Text&quot;,&quot;Title&quot;,1,48)\r\n\r\n<\/pre>\n<p>the remaining&#8230;<\/p>\n<p><figure id=\"attachment_1008\" aria-describedby=\"caption-attachment-1008\" style=\"width: 387px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Question.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1008\" alt=\"[System.Windows.Forms.MessageBoxIcon]::Question\" src=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Question.png\" width=\"387\" height=\"160\" srcset=\"https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Question.png 387w, https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Question-300x124.png 300w\" sizes=\"auto, (max-width: 387px) 100vw, 387px\" \/><\/a><figcaption id=\"caption-attachment-1008\" class=\"wp-caption-text\">[System.Windows.Forms.MessageBoxIcon]::Question<\/figcaption><\/figure><figure id=\"attachment_1010\" aria-describedby=\"caption-attachment-1010\" style=\"width: 365px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Hand.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1010\" alt=\"[System.Windows.Forms.MessageBoxIcon]::Hand, Stop or Error\" src=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Hand.png\" width=\"365\" height=\"159\" srcset=\"https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Hand.png 365w, https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Hand-300x130.png 300w\" sizes=\"auto, (max-width: 365px) 100vw, 365px\" \/><\/a><figcaption id=\"caption-attachment-1010\" class=\"wp-caption-text\">[System.Windows.Forms.MessageBoxIcon]::Hand, Stop or Error<\/figcaption><\/figure><figure id=\"attachment_1011\" aria-describedby=\"caption-attachment-1011\" style=\"width: 380px\" class=\"wp-caption alignnone\"><a href=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Asterisk.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-1011\" alt=\"[System.Windows.Forms.MessageBoxIcon]::Asterisk or Information\" src=\"http:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Asterisk.png\" width=\"380\" height=\"161\" srcset=\"https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Asterisk.png 380w, https:\/\/michlstechblog.info\/blog\/wp-content\/uploads\/2013\/07\/Icon_Asterisk-300x127.png 300w\" sizes=\"auto, (max-width: 380px) 100vw, 380px\" \/><\/a><figcaption id=\"caption-attachment-1011\" class=\"wp-caption-text\">[System.Windows.Forms.MessageBoxIcon]::Asterisk or Information<\/figcaption><\/figure>&nbsp;<\/p>\n<p>All available icons consecutively<\/p>\n<pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\r\n&#x5B;system.enum]::getValues(&#x5B;System.Windows.Forms.MessageBoxIcon])|foreach {&#x5B;System.Windows.Forms.Messagebox]::Show(&quot;&#x5B;&quot;+$_.GetType()+&quot;]::&quot;+$_.ToString(),&quot;Message box Icons&quot;,&#x5B;System.Windows.Forms.MessageBoxButtons]::OK,$_)}\r\n\r\n<\/pre>\n<p>Michael<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes while a powershell script is running you want to show a MessageBox with a information or warning to the user. In Windows Powershell no Commandlet exists to show a Message Box. Nevertheless it is possible by using the .NET Windows.Forms.MessageBox class:-). First of all load the assembly. # Load assembly &#x5B;System.Reflection.Assembly]::LoadWithPartialName(&quot;System.Windows.Forms&quot;) To show the &hellip; <a href=\"https:\/\/michlstechblog.info\/blog\/powershell-show-a-messagebox\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Powershell: How to show a message box<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[253,254,133],"class_list":["post-990","post","type-post","status-publish","format-standard","hentry","category-windowsscripts","tag-messagebox","tag-msgbox","tag-powershell"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/990","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/comments?post=990"}],"version-history":[{"count":20,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/990\/revisions"}],"predecessor-version":[{"id":1030,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/posts\/990\/revisions\/1030"}],"wp:attachment":[{"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/media?parent=990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/categories?post=990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michlstechblog.info\/blog\/wp-json\/wp\/v2\/tags?post=990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}