PS C:\>

Unleash your script

Windows PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language built on the .NET Framework.

Use MahApps.metro built-in dialogs

Apply beautiful modal dialog into your project.

How to apply a theme on your GUI?

Some tips on how to apply a theme on your GUI ...

Sunday 14 October 2018

Click Event on Treeview

Sometimes PowerShell and WPF can be tricky to achieve what you want. Treeview controls is not making things any easier. Just for a simple click, you need to go through a lot of trouble.

Before continuing reading, let's introduce some variables:
$treeviewItem is a TreeViewItem controls ([Windows.Controls.TreeViewItem])
$TreeView is the TreeView controls that holds several TreeViewItem.
If you want an example of a TreeView, you can check out this link.

How about using Add_MouseDown({}) ?

You have the MouseDown() event, but it doesn't work pretty much like how we want it. Looks like some issues with WPF they say (on MSDN).  They instead propose to use the "Preview ..." event version. So let us forget about this option! Well, I had no complaints, so I just used the method they proposed.

How do I use those "Preview" thing methods?

When you use the method on your TreeView, just add preview right before like this:

You can see that when you click inside your control, there is always an event fired even if it wasn't a node that you clicked. That's the normal behaviour of the method. However, that's not likely what we want. We just need an event to be fired when a node is clicked.

What should be done to make it works?

Instead of the TreeView itself, you have to assign the method to each TreeViewItem Control. Like this:
  Write-Host "Preview Mouse down " $_.OriginalSource

If you haven't done any graphical customization inside your XAML [used a DataTemplate or something else], this should work. Otherwise, you would say: "What the heck is this dude? It doesn't even work! 😠"

Don't worry! To make it works, you need to collect the right type of Data, which is a [Windows.Controls.TreeViewItem]

By default, This:

  Write-Host "Preview Mouse down" $_.OriginalSource

Equals to this:
 [System.Windows.RoutedEventArgs]$e = $args[1]
 Write-Host "Preview Mouse down " $e.OriginalSource

It returns the source of the event, not the TreeviewItem itself. In some ways, you can directly use this solution depending on what you want to achieve. 

But it's better to say to our script: "Hey give me the right stuff!"
For that, we need to tell Powershell what kind of control he should be waiting for.
Basically, we need to say: "Give me the treeviewItem related to the control I clicked."

To achieve this, you need to do some little modification:

  [System.Windows.Controls.TreeViewItem]$sender = $args[0]
  [System.Windows.RoutedEventArgs]$e = $args[1]  
  Write-Host "Node clicked: " $sender.Header

This way, we can get the "sender" control and we can manipulate it the way we want.

So this is it! You shouldn't have any trouble with your Treeview now.

Want to go further?

If you want to enable right or left click only, just replace the method with:
Add_PreviewMouseLeftButtonDown({}) for left click
Add_PreviewMouseRightButtonDown({}) for right click

Next post will be some drag and drop event with WPF/PowerShell.
Thanks for reading! That's all folks! 😉


Monday 16 April 2018

PowerShell WPF - Customize TreeView Icon

       I have seen many people struggle with this so I decided to write something about TreeView in PowerShell. 

The script in this post is based on the project available here (written in C#). I simply rewrote it in PowerShell and added my personal touch.  He displayed a drive image if the TreevieItem was a drive and a folder image otherwise. I extended the principle a little; you can specify directly in your script which kind of image you want to use in which case.


Thursday 12 April 2018

Window LifeTime Events in PowerShell

       In this post I’ll explain the lifetime of your window from the moment it is initialized and the moment it's destroyed. I hope this post will help you achieve easily what you seek and could be time-saving in some case.

What I’m going talk about here?

«To find it you must look beyond what you see» Rafiki
Everything has its lifecycle 😊. So does your window in PowerShell. It follows the same rule as in C# when you load it in PowerShell. It’s well explained here.


Monday 9 April 2018

PowerShell_Charts - Introduction to LiveCharts

As we all know flat design is the new black with material design and etc. So basically we are following the trend here.

Displaying graph does not escape this rule. Personnally, I wanted to do something about this for a while but I had no idea where to start. I wanted something compatible with MahApps, WPF, PowerShell and be “pretty” at the same time. And then I found this website:

So I began my little research and found nothing on the web L or maybe I was unlucky. I started testing the assembly and what a surprise! It works pretty well with Mahapps and WPF!

You can see a little preview of the graph in action here:

The only problem is the “type” of data passed to those graphs. It’s quite difficult to understand for a non-developer. So I came up with the idea to start a series of tutorials for each graph presented on their website and how to handle them in PowerShell.

I’ve not yet tested the “Material design” but it should come really soon on this blog too.
The source code of the preview is available in my git repository for those who can't wait.
See you next time! 😉


Monday 2 April 2018

Powershell_WPF - Part V - Display Dynamic content

In this post, I will show you how to add dynamic content inside a WPF window in PowerShell. Most of the time, we display dynamic data inside a Datagrid or a Listview, but what if we want to create our own display for each item? 

Recently I came across this post: Let’s play with SNCF API and Powershell!” written by Etienne Deneuve. You can get a list of the next train in a specific train station.

What‘s that got to do with us?

The answer is simple, this script will return a dynamic result for each query. And that’s a good example to use. Here is a window with a datagrid to show what we have done up until now, and a new block to show what could be done with a dynamic display.


Monday 26 March 2018

Add multiple buttons in Datagrid

If you are reading this, that means you wanted to add a button inside a Datagrid in PowerShell. 
Let us suppose that you need to add one or multiple buttons inside a single column in a datagrid. Data will be loaded inside the gridview and at the same time: n-number of command buttons need to be added to each row. Something like this:


Monday 19 March 2018

Mahapps Custom Dialog

When you design your apps, sometimes you need to add a custom content to your dialog form. In this post, I'm going to show you how to create your own custom dialog with PowerShell using Mahapps.

Here is a preview of the result:

You can put whatever content you want inside the dialog. Each dialog in the picture is a custom dialog.


Monday 31 July 2017

Find missing driver or disabled device

At one point of our life, we all had an issue with a missing driver on our computer. A problem with the display not shown correctly because the driver installed is not the appropriate one. A problem with a network card or embedded WIFI card and so on …

GitHub link for this project:

Popular Posts

Join us on Facebook


Contact Form


Email *

Message *


Thank you for visiting my blog and reading this far.

I'm mainly focusing on scripting, software development and modern workpalce experiences with Microsoft Technology. I created this blog for the main purpose of sharing my knowledge. Hope you will appreciate my works and will help you in yours. You are friendly invited to leave any kind of feedback in the comment sections of my posts.

Please note that all posts on this blog are my own opinions and are provided "as is", without warranty of any kind.