Build a PowerShell systray tool with menus, sub menus and pictures

7 A+ A-

In this post I will demonstrate how to build a tool that displays context menu and sub menus in the systray bar with picture for each menus.

In a previous post I explained how to build a systray tool with PowerShell.

I also explained in another post how to add sub-menus to your systray tools.

I have been asked how to add a specific picture for each menu and sub-menu.

Now let's see how to proceed.

What do we want ?

The icon of the main systray tool is the below one:

The systray tool will contain the below context menus:

- Menu 1

- Menu 2

- Restart the tool

- Exit

Both Menu 1 and Menu 1 will display two submenus.

The restart part will allow you to close the systray tool and open it again 10 seconds later (in case of bug).

The Exit button will allow you to close the systray tool

We will add picture on submenus for Menu1.

See below an overview of the final result:

Create the main part

In this part we will create the main systray structure.

To create a systray menu we will use the Windows Form class, as below:


We will then proceed as below:

1. Add the NotifyIcon class in a variable:

2. Change the text displayed when you pass the mouse over the icon:

3. Set a icon for our systray tool and make the tool visible:

4. Hide the PowerShell window when the systray tool:

5. Force a garbae collection (to manage RAM usage):

6. Create an application context for it to all run within.

7. See below the result:

Ok, we have just the systray tool but no menus, let's add them.

Add menus and submenus

To add menus we will use the class ContextMenuStrip:


To add submenus we will use the class ToolStripMenuItem:


We will then proceed as below:

1. Create the main menu

2. Create submenus

3. Add the context menu object to our main systray tool object:

4. See below the result:

Now we have have a complete systray tool with menus and submenus, let's add some icon for each one.

Add icon to menu item

To add menu we will need to convert picture to a bitmap object.

For that we will use the class [System.Drawing.Bitmap] and integrate a picture file into it, as below:

1. Add icon to main menu

2. Add icon to sub menus

3. See below the result:

Add action to menu

The last step is to add action when you click on menu or submenu.

For that we will use the Add_Click event on each menu and submenu.

In the below example, we will add an action for each submenu. The action will be to display a message box.

See below the code:

WPF_Systray 3158189338260897839

Enregistrer un commentaire

7 commentaires

Thomas a dit…
Ce commentaire a été supprimé par l'auteur.
Thomas a dit…

End of the article is missing after "See below the code:"

Anonyme a dit…

Don't you also need a load assembly line at the start too?

Add-Type -AssemblyName System.Windows.Forms

Unknown a dit…

Hi. Simply beautiful code.
After adding the additional line specified in comments - to load windows.forms, I was able to run this script in Windows 10 ps 5.1
I am having two problems - however. And used process of elimination to find the two lines that cause the script to exit - or freeze.

The following line causes the running PS instance to exit.
$null = $asyncwindow::ShowWindowAsync((Get-Process -PID $pid).MainWindowHandle, 0

Commenting out the above line allows the script to continue
But this next line causes the session to appear to hang


If I comment out both lines, I see the icon. When I click on the icon I see the ‘you clicked the icon’

Advice, or any updates for 2022?

Thanks so much for posting this series. Well explained to get my feet wet on the topic.

Anonyme a dit…

with your script how can i create a button to show a repertory content ? as a windows tool bar ?

GamBit a dit…

Very nice!
Could you provide a fully working example to download?
Can you generate one of the two submenus dynamic with a loop please?
What dimensions are the Icons?

Anonyme a dit…

essayer $null = $asyncwindow::ShowWindowAsync((Get-Process -PID $pid).MainWindowHandle, 2

Accueil item



Learn KQL in one month

You want to support me ?

Mes articles en français

Books in French