Monday, February 18, 2019

Popup Message Box From Power-Shell..


Every Admin, Developer, Scripter need good customized pop box message box to their user PC screen and creating a message box in power shell is pretty simple command ([System.Windows.MessageBox]::Show('Hello')) we have but sometime you want to show customize box where you can pass message box Title and some pretty good color and that message box appear on user windows for some limited time and close automatic and popup message closing time you can pass as an arguments. You can customize window size as well. I found the very good function which help to popup pretty neat popup message box. May be this can help you.


1. [System.Windows.MessageBox]::Show('Hello')

2. [System.Windows.MessageBox]::Show('Do you want to do coding?','My Test Box','YesNoCancel','Error')

3. Customize popup message box power shell code.

################################### Pop-Up Message ###############################

Function PopupWindow([string]$message,[int]$seconds,[string]$title)
{
     Function ClearAndClose()
     {
        $Timer.Stop();
        $Form.close();
        $Form.Dispose();
        $Timer.Dispose();
        $Label.Dispose();
        $Script:CountDown=$seconds
     }

     Function Button_Click()
     {
        ClearAndClose
     }

     Function Timer_Tick()
     {

        $Label.Text = "$message $Script:CountDown seconds.$title"
             --$Script:CountDown
             if ($Script:CountDown -lt 0)
             {
                ClearAndClose
             }
     }

     Add-Type -AssemblyName System.Windows.Forms
     Add-Type -AssemblyName System.Drawing
     $Form = New-Object system.Windows.Forms.Form
     $Form.Text = "customized Box..."                                         #Display title here
     $Form.Size = New-Object System.Drawing.Size(550,100)   #change the popbox Size
     $Form.StartPosition = "CenterScreen"
     $Form.MinimizeBox = $False
     $Form.MaximizeBox = $False
     $Form.Topmost = $True
     $Form.BackColor = "black"

     $Label = New-Object System.Windows.Forms.Label
     $Label.AutoSize = $true
     $Label.Location = New-Object System.Drawing.Size(30,30)
     # $Label.Font = 'Segoe UI, 8.75pt, style=normal, Italic'
     $label.ForeColor = "Yellow"              # Change the message text color

     $Timer = New-Object System.Windows.Forms.Timer
     $Timer.Interval = 1000

     $Form.Controls.Add($Label)

     $Script:CountDown = 6
     $Timer.Add_Tick({ Timer_Tick})

     $Timer.Start()
     $Form.ShowDialog()


}

PopupWindow("Hello",10,"My customized Box")   #Calling Function here....



################################################################################

Note:- Please test script in your test environment before running in production directly.

No comments:

Post a Comment