Monday, October 23, 2017

#90 : How to display balloon popup with Powershell?

With Powershell, you can leverage nearly all features available for .NET applications. Sometimes you must have noticed a popup in the right corner that pops up and it contains icons associated with it. Such notification can be useful if your script is performing some task with your account logged in to the host.

You can see the video here with live demonstration:


Script:
#--Declaring the function that takes care of balloon popup --#
function Show-BaloonPopup([String]$Title, [String]$Message , [int]$Delay=5   )
{

    Add-Type -AssemblyName System.Windows.Forms
    $script:balloon = New-Object System.Windows.Forms.NotifyIcon

    $Icon = 'Info' 
    $balloon.Icon=[System.Drawing.Icon]::ExtractAssociatedIcon("C:\bin\Apps\Powershell\ico.ico")

    $balloon.BalloonTipIcon  = $Icon

    $balloon.BalloonTipText  = $Message
    $balloon.BalloonTipTitle = $Title
    $balloon.Visible         = $true
    $balloon.ShowBalloonTip($Delay*1000)


    Start-Sleep -s $Delay 
    $balloon.Dispose()
} 

#--Calling the function with appropriate parameters --# 
Show-BaloonPopup "My Title" "This is the message. You operation has been completed." 5 


Output: 
You may notice a popup something like below. It appears for 5 seconds and disappear. We have added this logic due to reason that we don't have further need to have icon. If you remove the part of script that dispose the variable, the icon will appear forever and it will only go unless the system is rebooted or re-logged in.



Hope, it was east to implement. Please post comments if you have any difficulty implementing it. 
Thanks for reading this article. 




4 comments:

  1. only admins can use this function!!!!!

    ReplyDelete
  2. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work! ลูกโป่งเชียงใหม่

    ReplyDelete
  3. I love the way you write and share your niche! Very interesting and different! Keep it coming!
    SharePoint ticketing system

    ReplyDelete

#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...