- Create a certificate-signed RDP shortcut via Group Policy - Fri, Aug 9 2019
- Monitor web server uptime with a PowerShell script - Tue, Aug 6 2019
- How to build a PowerShell inventory script for Windows Servers - Fri, Aug 2 2019
Since PowerShell is built on top of the .NET framework, it can use nearly everything that the .NET framework has to offer. This means that you can build Windows Forms (WinForms) and Windows Presentation Foundation (WPF) GUIs from right inside PowerShell.
In this article, I will cover building a simple GUI with WinForms. However, if you would also like to learn about building WPF GUIs in PowerShell, take a look at Boe Prox's article, entitled “PowerShell and WPF: Introduction and Building Your First Window”. It provides a great starter tutorial on using WPF with PowerShell.
To get started building a WinForms GUI, we first need to ensure the .NET assembly is added. Since, natively, PowerShell does not support WinForms, we need to manually add that capability to our session. To do that, we will use the Add-Type cmdlet and specify the WinForms .NET assembly name.
Add-Type -AssemblyName System.Windows.Forms
Once loaded, we can instantiate WinForms objects and get started!
WinForms, as you might expect, are built on forms. Forms are essentially the foundation on which you place other objects, which are called controls. The first task when building a form is to instantiate a form object. To do this, you will use the New-Object cmdlet and specify a type of System.Windows.Forms.Form.
$Form = New-Object System.Windows.Forms.Form
As with all objects, the form object has various properties and methods. You can see that there are 194 properties and methods I can use. I encourage you to explore them to see what they can do.
However, our form object also has events. These are not available on your run-of-the-mill objects. Events are essentially triggers. Events allow you to execute code when something happens on your form. For example, you can see below that $Form has a Click event. This event is fired whenever the form is clicked on with the mouse.
Events are associated with nearly every object in the Windows.Form library and will be a big part of any larger WinForms project. But for now, we will keep it simple and just focus on building a form and adding a few graphical elements to it.
Now that we have built our form, how do we actually see it? We will use the ShowDialog() method.
That is a pretty boring GUI! Let's add an object to it. Since each GUI element is an object, I need to create them one at a time. I would like to create a label called “Title.” To do that, I will create a label object and change its Text property to what I would like to be shown on the form. Once created, I will need to add the object to the form.
Above, you will see how an object can be created, properties changed and a control added to the form. When ShowDialog() is executed, your form will look like the image below with a label entitled “Ticket title” at the far right-hand corner of the form.
There are hundreds of other objects that can be added to your forms. Each object has a common set of properties, such as location, which indicates where on the form the object should show up, as well as properties that are unique to the object itself.
Subscribe to 4sysops newsletter!
I have barely scratched the surface on what you can do with WinForms. The example I provided was simple. Imagine having to create dozens of different objects, messing with the location coordinates and so on for each. This way is not scalable. This is why most people choose to purchase a WYSIWYG tool. Sapien's PowerShell Studio is the best tool for the job. It allows you to drag and drop controls onto forms and change properties for everything via GUI. It is a much better experience than just building the objects by hand as I have here.