Navigate / search

PowerShell – How to build a GUI with Visual Studio

Sometimes PowerShell scripts are a way to comlicated to present your hardly gathered data to end users that are not familiar with the command line. Therefore I recommend to present data by Windows WPF forms that could be achived without being a coding expert.

With the help of Visual Studio everyone is able to build WPF forms even without coding expertise. In this tutorial I will show you how to generate a User Information script displaying Hostname, Username and Domainname. This is a basic tutorial showing how to build a graphical user interface within minutes. The script can be extended easily, for example you can display “Model Type” of your Hardware, “Network Drives” connected, “Outlook Profile” size, installed software and a lot more!

Visual Studio can be downloaded for free at Microsoft – https://www.visualstudio.com/downloads/

 

home Start a Project

Start Visual Studio (Express / Community / Professional) and open a “New Project …

Visual Studio – New Project

Select “WPF Appplication” and confirm with “OK“.

Visual Studio – WPF Application

Result:

The result is an empty WPF form that will be extended within the next steps.

Visual Studio – Workspace

plus Add Labels, Textboxes and Close Button

Add four labels to your form as in the picture below. If the Toolbox on the left side isn’t available on your Visual Studio installation you could reopen this element by clicking “View –> Toolbox“.

Visual Studio – Add Label

Add three textboxes to your form.

Visual Studio – Add Textboxes

Add a close button to your form.

Visual Studio – Add Button

Result:

Visual Studio will automatically generate the following XAML code that can be used in PowerShell.

To generate a WPF in PowerShell we can use the following script as a good start.

Before we can paste our Visual Studio XAML code into the PowerShell script we have to perform some small modifications:

  • Change line 1 from  <Window x:Class="WpfApplication9.MainWindow"  to  <Window
  • Remove line 6 and 7
  • Line 10 to 17 – Rename  x:Name=  to  Name=

After modification just paste the XAML code to your PowerShell script in line 4.

Result:

Save your PowerShell script and execute it. If you used PowerShell ISE to built your script just press F5 and you will see the following result.

Your Application – Main Window

check Congratulations you built your own PowerShell GUI!


plus Add Close Event

Now that we are able to display our form we should get the close button running first. We just have to assign a event to our button that is named “button” by default.

You are now able to close you application by clicking 


plus Add System Variables

To receive the Hostname, Username and the Domain we can use the following commands:

Just add the following marked lines to your code and you are done with your first PowerShell WPF Form.

Result:

Your Application – Working Variables

Feel free to modify your XAML code in Visual Studio to get better user experiences. As an example I’ve created the following XAML code.

As you can see the result looks different to the form before and it just took a few minutes to achive this result in Visual Studio.

Your Application – Final Result

Leave a comment

name*

email* (not published)

website

This site uses Akismet to reduce spam. Learn how your comment data is processed.