In this article, I'll be talking about how to use a custom icon for your PowerShell GUI.

By default, running your PowerShell script that shows a WPF UI, the titlebar of your app will use a default PowerShell Icon, like the one shown below.


Thankfully you can change this, using some simple code. Unfortunately, we can't just reference an image name, even if it's in the same root folder our script runs from, we first need to determine where our script is running from. Of course you can just hardcode your path, ex: "C:\myscripts\someicon.ico", but as with all hardcoding, you're bound to run into issues at some point. The best course of action then is to get our application path. We can achieve this with the following code:

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$myIconPath = Join-Path $scriptPath 'logo.ico'

You can see above we are using $MyInvocation.MyCommand.Definition to get our current path. The next line appends our icon file name to the path. Just a note, I am using am .ico file here, but a .png file will work just as well.

Now that we have our path to our image, we need to tell the UI to use it. Unfortunately, we can't just set our UI's Icon property, we need to rely on a Lifecycle event to make it happen. The code below adds a Loaded lifecycle event that triggers when the window is loaded, but not yet rendered, and when this happens, we set the Windows Icon property to our full path to our image. If you want to know more about the various WPF Lifecycle events, I found this article useful:

    $Window.Icon = $myIconPath

The last thing we need to do is show our Window as normal.

$Window.Showdialog() | Out-Null

Now our application titlebar should look like this:


You can take a look at the complete source code on my github page here: