PowerShell is widely known for its command line prowess. It is a scripting language that allows you to build many useful tools via the command line. However, did you know that you can also build tools with a graphical user interface (GUI) in addition to all of those cool command line tools you have been working on

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.

194 properties and methods

194 properties and methods

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.

$Form has a Click event

$Form has a Click event

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.

Form without content

Form without content

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.

Changing the Text property

Changing the Text property

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.

When ShowDialog() is executed

When ShowDialog() is executed

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.

  1. Mike Shepard 6 years ago

    An alternative to commercial tools is PoshGUI.  Not as full-featured, but we probably shouldn’t be building “full-featured” GUIs in PowerShell anyway.

    Still in development, btw.

  2. john taylor 6 years ago

    PoSH Gui Builder is pretty slick too: https://poshgui.com.

  3. Beatrice 5 years ago

    Thank you for sharing very useful

Leave a reply

Your email address will not be published.


© 4sysops 2006 - 2022


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account