Skip to content

Instantly share code, notes, and snippets.

@matthewjberger
Last active June 21, 2016 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewjberger/4829ad72e02ad5cf695eb1fe975581dc to your computer and use it in GitHub Desktop.
Save matthewjberger/4829ad72e02ad5cf695eb1fe975581dc to your computer and use it in GitHub Desktop.
message boxes in powershell
<# types
"ok",
"error",
"warning",
"question",
"info",
"okcancel",
"abortretryignore",
"yesnocancel",
"yesno",
"retrycancel",
"canceltryagaincontinue"
#>
<# buttons
0: OK
1: OK Cancel
2: Abort Retry Ignore
3: Yes No Cancel
4: Yes No
5: Retry Cancel
#>
# Example Usage:
# ShowMessage "Test Message" "My Title" "yesnocancel"
function ShowMessage($msg, $caption="Notice", $type="info", $buttons=-1)
{
$wshell=New-Object -comObject Wscript.Shell
$typeInt = 0
switch($buttons)
{
0 { $type = "ok" }
1 { $type = "okcancel" }
2 { $type = "abortretryignore" }
3 { $type = "yesnocancel" }
4 { $type = "yesno" }
5 { $type = "retrycancel" }
}
switch($type)
{
"error" {$typeInt = 16} # 'Stop'
"warning" {$typeInt = 48} # 'Exclamation'
"question" {$typeInt = 32} # 'Question'
"info" {$typeInt = 64} # 'Information'
"ok" {$typeInt = 0} # 'Ok'
"okcancel" {$typeInt = 1} # 'Ok Cancel'
"abortretryignore" {$typeInt = 2} # 'Abort Retry Ignore'
"yesnocancel" {$typeInt = 3} # 'Yes No Cancel'
"yesno" {$typeInt = 4} # 'Yes No'
"retrycancel" {$typeInt = 5} # 'Retry Cancel'
"canceltryagaincontinue" {$typeInt = 6} # 'Cancel Try-Again Continue'
default {$icon = 0} # 'No icon'
}
# 0 for the second parameter (duration) here means to stay up until closed (i.e. 'infinite' duration)
$answer = $wshell.Popup($msg,0,$caption, $typeInt)
$returnValue = ""
switch($answer)
{
1 { $returnValue = "ok" }
2 { $returnValue = "cancel" }
3 { $returnValue = "abort" }
4 { $returnValue = "retry" }
5 { $returnValue = "ignore" }
6 { $returnValue = "yes" }
7 { $returnValue = "no" }
10 { $returnValue = "tryagain" }
}
return $returnValue
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment