Create a GUI for a PowerShell Script

There are a few ways that you can create a GUI for a PowerShell script, My preferred way is to use WPF forms. This is because you can use Visual Studio to create the GUI then call the GUI into a script. There are some draw backs when using WPF forms but in most cases the Pros out way them.

To get started you will want to download Visual Studio if you do not have a license you can use visual studio community edition it is free for independent developers, researchers and Open-Source Developers.

Once you have downloaded the installer run it, the installer will download the needed files then prompt you to choose workloads for this you only need .net desktop development


The install will take some time this is a perfect time to refill your coffee before we really get into it.


Once installed you will be prompt to sign in, this is just a Microsoft account you will need this to keep using the community edition. Then you can choose the theme, I personally like the dark theme. After that you will be taken to the start page. Now that Visual Studio is installed we can get started.
to start click file > New > Project from the new project window choose WPF App (.NET framwork)
Give your project a name and choose where you want to store it


When the Project opens you will be present with the design view and XAML code split in the editor. The XAML is the code of your GUI this is what PowerShell will call to show the GUI.


First lets give the form a good title, you can do this by either editing the is line in the code


or by editing the title in the properties window



Now that we have the name of the GUI lets change the size of the window so something little smaller This can be done by editing the height and width in the same line as the title in the XAML or by searching for the height and width in the properties box.


Lets add a label telling the user what this is. from the left side choose the tools tab and expand the all WPF Controls section


Now drag the label control to the design where you would like it


You can change what the label says by editing the content line of the xmal or the content in the properties box


Next lets add a button to the design go back to the tools control and drag a button to the design


Be sure that any item that will be changed or doing something has a unique name so that it can be called by your script later on.


Now add a textbox control to the design


After you add the text box be sure to save your project 

At this point this is what your xaml and design should look like


from here we can close Visual Studio

For the next part you will need something to call the XAML file into PowerShell to show the GUI you created. We have already created this and can be downloaded from our GitHub page. For this project you will want to open the call-XAML.ps1 file so you can modify the path to the xaml file you created in Visual Studio.

To call the XAML file change the path on line 5 to the path of your XAML file in the Visual Studio project. For me that is C:\Users\kgross\source\repos\FirstGUI\FirstGUI\MainWindow.xaml


Comment line 28 then run the script


When you run the script you should see he GUI you created as well as some items in the PowerShell host


The items you see in the PowerShell host are the names of the items we created in Visual Studio there all are variables that can be used in the PowerShell script . Now that we have verified that the GUI works lets make it do something.

In the call-XAMLFile.ps1 script lets add a button click event, This will tell the script to do something when the button is clicked. This is done with the below code


$WPF_Firstbutton.add_Click({
    
})

Between the brackets add the following


$WPF_textBox.text = $ENV:USERNAME

The code will add your user name to the text box when you click the button.

At this point your code should look like this


Now run the script and give it a try


Thats it you now have a gui to for a PowerShell script

Once you have a nice GUI you will want to package your script into an exe. with our PS2EXE-GUI tool you can do this with ease. Head over here to see how it is done

Comments