Powershell pop-up window like a king

How many times have you just googled  “how to do pop-up window with Powershell?” for me its every time I do new script, always forget. Then a friend of mine showed his function yesterday, and its to good to not share this. 🙂
So here’s he’s GitHub https://gitlab.com/Popeen/EasyGUI/tree/master

The function is here:
New-Popup.zip

function New-Popup{
#This is using VBScript popup, more info about it can be found here https://ss64.com/vb/popup.html
param(
[String]$Title = "",
[String]$Text,
$Type = 0,
[Switch]$Type:OkOnly,
[Switch]$Type:OkCancel,
[Switch]$Type:AbortRetryIgnore,
[Switch]$Type:YesNoCancel,
[Switch]$Type:YesNo,
[Switch]$Type:RetryCancel,
$Icon = 0,
[Switch]$Icon:Critical,
[Switch]$Icon:Question,
[Switch]$Icon:Exclamation,
[Switch]$Icon:Information,
[Switch]$ReturnText
)

switch($Type){
"OKOnly" { $Type = 0 }
"OkCancel" { $Type = 1 }
"AbortRetryIgnore" { $Type = 2 }
"YesNoCancel" { $Type = 3 }
"YesNo" { $Type = 4 }
"RetryCancel" { $Type = 5 }
default { if($Type -notmatch '^[0-9]+$'){ $Type = 0 } }
}

switch($Icon){
"Critical" { $Icon = 16 }
"Question" { $Icon = 32 }
"Exclamation" { $Icon = 48 }
"Information" { $Icon = 64 }
default { if($Icon -notmatch '^[0-9]+$'){ $Icon = 0 } }
}

$stringReturns = @{
"1" = "OK"
"2" = "Cancel"
"3" = "Abort"
"4" = "Retry"
"5" = "Ignore"
"6" = "Yes"
"7" = "No"
}
$return = (New-Object -ComObject WScript.Shell).Popup($Text, 0, $Title, $($Type+$Icon))

#If $Type+$Icon is a number that VBS Popup don't support no popup is shown, if so show an OkOnly popup without any icons
if($return -eq $NULL){
$return = (New-Object -ComObject WScript.Shell).Popup($Text, 0, $Title, 0)
}

if($ReturnText){
$return = $stringReturns["$return"]
}

return $return
}

 

 

Heres some examples how you can use it

New-Popup -Title "Good title!" -Text "What a nice weather there is in sweden.." -Type:OkOnly -Icon:Information

 

New-Popup -Title "God is calling" -Text "Do you want to change weather to sun again?" -Type:YesNo -Icon:Question

 

New-Popup -Title "You failed God!" -Text "You didnt change weather in time, you gonna have rain rest of the year. Muahah.." -Type:RetryCancel -Icon:Critical

 

New-Popup -Title "God forgive you!" -Text "Just kidding, Here you have sun for the rest of your life, have a beautiful day!" -Type:OkOnly -Icon:Exclamation

Good luck with using this.
Cheers
Pontus

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s