How to create a Windows Forms application with PowerShell
Posted on July 6th, 2018
I do know about you, but speaking for myself now a days I do not like having to install large development programs/IDEs on my personal or work device. I would much rather RDP to a server or access an online development environment, usually these options have more storage. I just think that installing a development environment on my work or personal device takes up too much memory. Memory I could be using for something else.
Plus, any half decent modern computing device will come with a Solid State Drive, which is great, but the more memory you want the more you pay. So, I get around this by simply choosing a minimum storage capacity of 256GB SSD and using mainly cloud storage and for other data a 1TB external SSD. This seems to work well for me.
With that said, I like to script. I like to automate processes using scripting. The bonus is that the IDE for PowerShell and Batch file creation is very light weight. For Batch files, I use the new light weight code editor from Microsoft Visual Studio Code, and the PowerShell IDE is built-into the Windows Operating System.
Recently, I have been creating many PowerShell tools for level 2 and 1 IT staff, so I have been getting more proficient with PowerShell. Upon some PowerShell research I stumbled upon some tutorials on how to create a GUI based PowerShell script. At the time I bookmarked some tutorials for later review.
It wasn't until I saw the Driver Automation Tool from the folks over at SCConfigMgr, which is a tool completely written using PowerShell, that I wanted to start creating my own PowerShell tools with a GUI.
Well, after reviewing my bookmarks I started to see a trend that you would need to download Visual Studio Community to use the XAML editor to create the UI or hand code each System.Window.Forms object into the PowerShell script. This would not work for me 1) I don't want to download Visual Studio Community Edition and 2) Who wants to hand code each Window Form object in there PowerShell script? I don't have time for that :-)
Just when I thought all hope was lost I said, "Self, let's to one more Google search to see what's out there, and if there are not any results that will work for me then I will just continue to script the tools without a GUI."
That last Google search allowed me to stumble across a very cool website/tool that allows you to create a PowerShell based UI from a web browser.
To start using PoshGui navigate to PoshGui.com and setup an account.
Let's create a "Hello, World" PowerShell UI application.
PowerShell Hello, World UI Application
Click on the GUI Editor.
Give the form the name HelloWorldFrom.
Place a button in the form.
Give the button the name HelloBtn.
Change the button text to Say Hello.
Under layout check Right, Left, Top, and Bottom.
While the button is selected, click on Events to the right of Properties.
Place a check mark by the Click event.
Click on the <> icon, and give the event the following function name SayHello, press OK.
Now place a label above the button object on the form and give the label the name GreetingLabel, with NO text.
Click on the code option and copy the generated code.
The generated code should look similar to the below code:
After copying the generated code open the PowerShell ISE and paste the code in a new file.
Notice there is a line in the generated code that says #Write your logic here. This is where the SayHello function and logic will be written. Lets do this now.
Delete the line #Write your logic code here and enter the following code.
Now the entire code should look similar to the below code:
Now run the script in the ISE. The below Window should appear and when you click the button the label text should appear as Hello, World!.
Now go and build something great with this new found knowledge :-)