Hey, Scripting Guy! Weekend Scripter: Creating a GUI for a Windows PowerShell Script


Bookmark and Share

Microsoft Scripting Guy Ed Wilson here. When I showed the Scripting Wife the Get-FuelCostFunction.ps1 script from yesterday, she was less than enthused. She did not like the idea of opening up either the Windows PowerShell ISE or the Windows PowerShell Command Prompt window. She wanted a graphical interface and an icon she could click.

At first, I thought about creating an input box and letting her type the information in there, and then display the results with a message box. However, because it is the weekend, I decided to break out my copy of SAPIEN’s Primal Forms Community Edition and play around with that. As seen in the following image, the interface is quite a bit like Visual Studio. You drag buttons, labels, and text boxes to the form.

Image of SAPIEN's Primal Forms Community Edition GUI

After I had the interface built, I saved the form, and then exported it as Windows PowerShell code. The next thing I needed to do, was to open the Windows PowerShell code in the Windows PowerShell ISE and see if the code would execute. When I ran the Windows PowerShell code, the Windows form that is shown in the following image appeared.

Image of Windows form generated by script

Now that I have a basic Windows form, it is a simple matter of adding some code. The nice thing about Primal Forms is it adds comments to the section of code you need to modify. This is shown here where I add code that displays the computed price of the trip. The value that is entered in the miles text box ($tb_miles) is assigned to the text property. This is also true for the miles per gallon ($tb_MilesPerGallon) text box and the cost per gallon ($tb_CostPerGallon). It is therefore a simple matter to state that the text of the Total Cost text box ($tb_TotalCost) text property is equal to this formula: ($tb_miles.text/$tb_MilesPerGallon.Text) * $tb_costPerGallon.Text. To display only two decimal places in the text box, the “{0:n2}” format specifier is used. This section of the script is seen here:

#Generated Event Script Blocks
#Provide Custom Code for events specified in PrimalForms.
#TODO: Place custom script here
 $tb_TotalCost.text = “{0:n2}” -f (($tb_miles.text / $tb_MilesPerGallon.Text) `
  * $tb_costPerGallon.Text)


The script is now run from inside the Windows PowerShell ISE, to ensure it works properly. This is seen in the following image.

Image of script run in Windows PowerShell ISE

The last thing I need to do to get out of the doghouse from my kitchen episode yesterday is to make the Windows PowerShell script execute when you double-click the script. This actually involves a trick, because by default when you double-click a Windows PowerShell script, it opens in Notepad. To do this, create a custom shortcut on the desktop and point it to the script. Modify the target of the shortcut to be Windows PowerShell and place the path to the script in quotation marks, as shown in the following image.

Image of creating custom desktop for Windows PowerShell script

That is a quick look at creating a graphical interface for a Windows PowerShell script. As you have seen, we did not use very much of the original script from yesterday. The complete graphical trip cost calculator Windows PowerShell script is available on the Script Repository.

If you want to know exactly what we will be looking at tomorrow, follow us on Twitter or Facebook. If you have any questions, send e-mail to us at scripter@microsoft.com or post your questions on the Official Scripting Guys Forum. See you tomorrow . Until then, peace.


Ed Wilson and Craig Liebendorfer, Scripting Guys 


Discussion is closed.

Feedback usabilla icon