New-WPFMessageBox -Content "The script ran into a problem that it couldn't handle, and now it needs to exit. Or we can create a Windows 10 BSOD-style error: In this code, I am using a try / catch block to catch an error, then using the message box to display it. New-WPFMessageBox -Content "$_" -Title "PSSession Error!" New-PSSession -ComputerName $ComputerName -ErrorAction Stop Maybe you prefer a little more drama in your error message box. The sound will be played when the message box is displayed. This is a dynamic parameter allowing you to select from available Windows sounds in your local media library. In this one I’ve added the Sound parameter. New-WPFMessageBox -Content "There was a problem connecting to the Exchange Server. New-WPFMessageBox -Content "The file could not be opened." New-WPFMessageBox -Content "The server has been rebooted at 08:35 on 16th January 2017" If you define the parameters once in your script, then you can simply call them everytime you want to display that message type. New-WPFMessageBox can create Information, Warning or Error message boxes using your own style. Do you want to go ahead?"ĬustomButtons = "Yes, go ahead!","No, don't do it!", "Save it for later" $Params = "The purchase order has been created. Set the ButtonType to None, and pass the text for your buttons to the CustomButtons parameter: You can also add your own custom buttons. When a button is clicked, the text of the button is saved to a variable named WPFMessageBoxOutput so you can handle this in your code and perform different actions depending on what was clicked, eg:ĮlseIf ($WPFMessageBoxOutput -eq "Retry") New-WPFMessageBox -ButtonType Abort-Retry-Ignore $Params = "The file 'Important Document.docx' could not be deleted." Simply select an option using the ButtonType parameter. The default button is the OK button, but you can also use other buttons or button combinations such as Yes/No, Retry/Cancel, Abort/Retry/Ignore etc. You can change the button color using ButtonTextForeground: New-WPFMessageBox ContentBackground to change the main content background:
When using several parameters, it can be easier to ‘splat’ them like so: The Content… parameters allow you to change the main content section, and the Title… parameters allow you to change the title. The FontFamily parameter is also dynamic and allows you to select from fonts available in. You can change the fonts, font weights and font sizes, as well as the background and foreground colors to create some nice combinations. New-WPFMessageBox -Content "I'm a WPF Object!" -Title "Custom Message Box" -TitleBackground CornflowerBlue This parameter is a dynamic parameter and will allow you to select from the list of available colors in the. Use the TitleBackground parameter to change the color. The title bar is a color block, so it can be used to create a nice contrast with the main content, for example. New-WPFMessageBox -Content "I'm a WPF Object!" -Title "Custom Message Box" To add a header, simply use the Title parameter: The Content parameter is the only mandatory parameter, and can take either a simple text string or your own custom WPF content (which we’ll look at later). New-WPFMessageBox -Content "I'm a WPF Object!" To create a simple message box, all you need to do is pass a string to the Content parameter: Let’s have a look at some ways we can use this function. The message box has a basic default design, but if you’re the creative type then you can customize away until you get an appearance you like. My goal was to create a good-looking, fully-functional message box window that is highly customizable in the WPF format, and can even be used as a window host to display custom WPF content.
So I decided to create my own customizable message box in WPF as a PowerShell function that can be used in my scripts and UI tools.Įnter New-WPFMessageBox. In this age of modern design languages like Metro and Material Design however, this message box is starting to look a little dated. I’ve used it in my PowerShell scripts as well as my UI tools and even in my ConfigMgr task sequences for providing feedback to the end user.īoth WinForms and WPF also have a message box but they have similar style and functionality to the VBScript version. The humble VBScript message box has been around for a while now and is still a useful way of providing a simple pop-up notification in Windows. Update | Changed the way the output from a button click is handled, saving it to a variable instead.