Create your own PowerShell systray/taskbar tool


In this post I will show you how to create your own Systray tool with PowerShell and how to load some WPF content.

Before to start, you can find a really nice post from Chrissy Lemaire about that here.
I learned with this one.
I wanted to do my own in order to explain how to proceed for everyone.
So let's start.

What is a systray tool ?
Systray tools from Windows or other soft work as below most of the time.
We will create our own tool with the same behavior.
- Right click with different options
- Options are displayed like context menus
- When you click on a menu a Window is displayed
- Sometimes when you click out of this Window, it will be closed

What do we want ?
In our first systray tool we want:
- Diplay an icon in the Taskbar
- Display different context menus after a right click on the icon
- Display a window when you click on the context menu
- If you click out of the window, it will be hidden

How to get the sample ?


Create the systray icon
We will load some assemblies like forms and also Mahapps for later.
To create the systray menu we will create an object System.Windows.Forms.NotifyIcon
We will first add an icon to the systray. 
See below the code used to do that.
In this code I choose the below icon.

You can choose another one by checking .exe in C:\Windows\System32 for instance, as below:

This code will just add the icon in the systray, we won't have any context menus or actions, so now we'll add them.

Add context menus
Now we want to display some context menus after doing a right-click on the icon.
In this sample my menu is composed of four parts:
- Users
- Computers
- Restart the tool
- Exit

See below how to do that:
See below the result:


Add GUI to display
Now we want to display two different GUIs after clicking on the Users and Computers buttons.
For that, we'll create two XAML part in our code as below (I just added the User part).

Add actions on menus
Ok for now we have: 
- A systray icon 
- Different context menus 
- GUI to display on context menu 

However, we don't have any  actions after clicking on our context menus.
To do that it's pretty simple, just consider your menu as a button meaning we have to use a specific event for our menus. 
In our example, we want to display a GUI once we have clicked on a menu, meaning we'll use the event Click.
See below the code use to add our actions on Users and Computers menus.
Now when we click on a menu a GUI is displayed, as below:

It's cool but if we click somewhere else out of the GUI, it's still here, as below:

For that we will add another action with another event called Deactivated meaning if the GUI lost the focus it will be closed.
See below the code part to do that:
See below the result in action:

Now we will add another event in our GUI in order to prevent people to close the GUI.
For that we will add the event Closing on our GUI, as below, and we will display a message if someone click on the Close button.
See below the code part to do that:
See below the result:

Now we will add an action to the restart menu.
What do we want ? We want to close the systray tool and start it again after 10 seconds.
For that we will add the part below:

And we will add the part below at the beginning of the script:
See below the result:


In the next post we will see why some controls like TextBox may not work with systray tools like this and how to resolve that.



Share this

Related Posts

Previous
Next Post »