Message boxes can be both annoying and informative.
Let’s make some message boxes using different methods!
Let’s start with this method first as this is a nifty one.
This method is easy to both read, understand, and modify. It is also easy to implement third party processes if you’re handy with code.
Another thing that is great with this is that the script will let you know which button is clicked. This is actually great for sending “yes” and “no” questions to remote users.
Use the charts below to customize the message box to make it perfect for the purpose of your message.
Add-Type -AssemblyName PresentationCore,PresentationFramework $msgBody = "Are you happy?" $msgTitle = "Happy-O-Meter" $msgButton = 'YesNo' $msgImage = 'Question' $Result = [System.Windows.MessageBox]::Show($msgBody,$msgTitle,$msgButton,$msgImage) Write-Host "The user clicked: $Result "
Buttons to choose from
|OK / Cancel||OkCancel|
|Yes / No||YesNo|
|Yes / No / Cancel||YesNoCancel|
Icons to choose from
This method does only have a body text, and a title header. It will not contain any icons and only have an OK button under the body text. It will on the other hand let you know if the OK button is clicked.
PowerShell -Command "Add-Type -AssemblyName PresentationFramework;[System.Windows.MessageBox]::Show('The great message')"
The VB Method
There is also the good old way that has been around for many many years.
This method is quick, but comes with some downsides.
First off, it won’t notify you if the user clicks the OK button.
It ONLY comes with an OK button.
Your username will appear in the header/Title along with a timestamp of the time the message was sent.
AND, if you try this method in a local domain network, it could get messy.
(True story as i have gotten to experience that by accidentally forgetting to change a tiny character)
But if you want to give it a try it’s the following code:
msg * Hello There!
Okay here is the thing about this command.
You can execute it in either PowerShell or CommandPrompt, but be aware of the “*” symbol. This character indicates the username you want to send the message to. So if the username is “jbrekke” or “janb” that is what should be there.
Using the “*” simply selects every user on the computer. In a Domain network this would mean every user there! So in my little accident i told the user to call me, and man did i get a lot of calls that day. Luckily the message box has a timer so it will disappear after some time.
Windows “Balloon” Box
This is the new and fancy way to display messages.
This will as you have guessed pop up a message balloon from the notification area.
You can use all the same icons in this method like we did in the first message box we had a look at in this article.
On the 6th line of the code you’ll see the “TimeOut” value set as 10000. According to Microsoft, this value is in milliseconds, but i think the operating system has an override to this as it won’t change duration no matter what value i set here. Different from the message box above, this method doesn’t have any buttons.
[reflection.assembly]::loadwithpartialname('System.Windows.Forms') [reflection.assembly]::loadwithpartialname('System.Drawing') $notify = new-object system.windows.forms.notifyicon $notify.icon = [System.Drawing.SystemIcons]::Information $notify.visible = $true $notify.showballoontip(10000,'Header Title','Hey look at me!!',[system.windows.forms.tooltipicon]::Info)
If you need to change the icon on the balloon simply change the “Info” on the 6th line to either
The same goes for that little icon you would see on there.
This can be changed by changing the word “Information” located on the 4th line to either
This one is the same as above, but as a one-liner that you can execute from either PowerShell or Command Prompt.
It is a bit harder to read, but if you study the command you’ll see all the previous sections.
As with the previous one, you can change the Icons as you wish by changing the value.
Change the “Information” word to change the small icon, and the “None” word to add/change the big icon.
powershell -WindowStyle hidden -Command "[reflection.assembly]::loadwithpartialname('System.Windows.Forms');[reflection.assembly]::loadwithpartialname('System.Drawing');$notify = new-object system.windows.forms.notifyicon;$notify.icon = [System.Drawing.SystemIcons]::Information;$notify.visible = $true;$notify.showballoontip(10,'One-Liner Balloon','This is how the one-liner works',[system.windows.forms.tooltipicon]::None)"
We hope this was informative, and or helped you out in a way.
Until next time; Stay curious, and learn something new every day!