Adding Toolbar Icons to Your PowerShell WPF GUI

This post builds on some previous post titled Use base64 for Images in WPF-based PowerShell Forms.

Show-WpfIconExample.ps1, which can be found below and on github, demonstrates how to overlay the default PowerShell toolbar icon with an image that’s been transformed to a base64 string to System.Windows.Media.ImageSource.

An icon overlay is the mini icon that sometimes shows up on top of a taskbar application icon.

Icon-Overlay

The overlay icon is controlled by $window.TaskbarItemInfo.Overlay. I’m actually unsure if the PowerShell toolbar icon can be replaced entirely ($window.Icon usually takes care of that) without a ton of code.

As a bonus, I also included how to change the taskbar popup description from “Windows PowerShell” or nothing to the title of the $window.

The secret sauce of this is
$window.TaskbarItemInfo.Overlay = $bitmap
$window.TaskbarItemInfo.Description = $window.Title

If you’d like to learn more about the flag of my people, the Cajuns, check out the Wikipedia article “Flag of Acadiana“.

Note that this code can’t be copy/pasted into the console because the PowerShell window will disappear before it can complete the paste. Be sure to download the script or paste the code below into a .ps1 file and execute. It does appear to work well in the ISE, though!

Up next, I’ll be talking more about running your WPF GUIs using ApplicationContext instead of $window.ShowDialog()

Chrissy is a PowerShell MVP who has worked in IT for nearly 20 years, and currently serves as a Sr. Database Engineer in Belgium. Always an avid scripter, she attended the Monad session at Microsoft’s Professional Developers Conference in Los Angeles back in 2005 and has worked and played with PowerShell ever since. Chrissy is currently pursuing an MS in Systems Engineering at Regis University and helps maintain RealCajunRecipes.com in her spare time. She holds a number of certifications, including those relating to SQL Server, SuSE Linux, SharePoint and network security. She recently became co-lead of the SQL PASS PowerShell Virtual Chapter. You can follow her on Twitter at @cl.

Posted in PowerShell, WPF

Leave a Reply

Your email address will not be published. Required fields are marked *

*