One of the simplest way to build simple message boxes in PowerShell is to “borrow” from .NET COM objects. Basically you initialize the object, and then you need to create instances of the object and finally call the Popup method. These messagebox code may be familiar to anyone who has programmed in vbScript, or any of the Microsoft programming languages such as Visual Basic or C#. The boxes can look pretty good, even though the coding is a bit monster.
In simple, all it really takes is two lines. One line to create the Wscript.Shell COM object and one to invoke the Popup method. Before the execution, let we discuss about the syntax below
Syntax :
intButton = objShell.Popup(strText,[nSecondsToWait],[strTitle],[nType])
Parameters :
strText | String value containing the text you want to appear in the pop-up message box. |
nSecondsToWait | It’s an optional. Maximum length of time to display the pop-up message box (in seconds, Optional, default=infinite) |
strTitle | Title text string, It’s an optional. |
nType | Type of buttons and icons (Numeric, Optional) These determine how the message box is used. Button Types : OK = 1, Cancel = 2, Abort = 3, Retry = 4, Ignore = 5, Yes = 6, No = 7 Icon Types : 16 – “Stop Mark” icon. , 32 – “Question Mark” icon, 48 – “Exclamation Mark” icon. , 64 – “Information Mark” icon. |
The command will write the return value of the clicked button to the pipeline as shown below
Button the return value :1 – OK button ,2 -Cancel button , 3 – Abort button , 4 – Retry button ,5 – Ignore button , 6 – Yes button , 7 – No button
Example #1:
1 2 3 4 |
#creating object os WScript $wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop #invoking the POP method using object $wshell.Popup("Are you want to continue from here?",5,"Hello User?",48+4) |
Script Explanation:
From the syntax, please find parameter values.
strText : Are you want to continue from here?
nSecondsToWait : 5 (wait for 5 second)
strTitle : Hello User?
nType : 48+4 (Exclamation Mark icon + Yes and No buttons)
IntButton : Get the selected input as show in below table
OUTPUT:
What do you think?
I hope you have an idea of How to display a pop-up message box with PowerShell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.
Leave A Comment