We have all seen the message boxes pop up when we are working with forms, online applications, software installs, etc. Today I am going to go through the different types of message boxes available within PowerShell. There are a number of elements that we can use to get the best functionality from our message box.
First things first, we need to load in the Powershell Assembly:
1 |
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null |
We can get the message box to display any type of message we want, using static text or even have it show a variable or part of string.
Here is the Powershell code to create a simple message box:
1 |
[System.Windows.Forms.MessageBox]::Show("This is my first message box.") |

As we can see from the message box above we have our message displaying in section 2 and an OK button in section 3. While this is a pretty basic message box it is quite useful when you want the end user to stop and think about what they might be doing. Section 1 is empty.
Next I would like to add a title for my message box, something to fill in section 1. By adding more to the above code we can define the title of the message box.
1 |
[System.Windows.Forms.MessageBox]::Show("This is my first message box.","My Title") |
Now we move on to the different types of message boxes. As I said earlier, there are 6 types. The type numbers we use range from 0 to 5. If we don’t use a type number the default is 0.
Type 0: Standard message box with just the OK button.
Type 1: OK and Cancel button message box.
1 |
[System.Windows.Forms.MessageBox]::Show("This is my first message box.","My Title",1) |
Type 2: Abort, Retry, Ignore message box
1 |
[System.Windows.Forms.MessageBox]::Show("This is my first message box.","My Title",2) |
Type 3: Yes, No, Cancel button message box.
1 |
[System.Windows.Forms.MessageBox]::Show("This is my first message box.","My Title",3) |
Type 4: Yes, No button message box.
1 |
[System.Windows.Forms.MessageBox]::Show("This is my first message box.","My Title",4) |
And finally, Type 5: Retry, Cancel button message box.
1 |
[System.Windows.Forms.MessageBox]::Show("This is my first message box.","My Title",5) |
All of the above message box types will return the value of the button selected. You can use this information to decide what happens next in your script.
1 2 3 4 5 6 7 8 9 |
$messageboxreply = [System.Windows.Forms.MessageBox]::Show("Do you wish to continue ?","Welcome",3) if ($messageboxreply -eq "Yes"){ Write-Host "Your choice was Yes"} elseif ($messageboxreply -eq "No"){ Write-Host "Your choice was No"} elseif ($messageboxreply -eq "Cancel"){ Write-Host "Your choice was Cancel"} else {} |
As always, please feel free to comment.
Typo: elseif ($messageboxreply -eq “Cancel”)
Thank you for spotting that – it has been corrected now.