Creating a GUI with Powershell

PowerShellThe Powershell scripting language helps extend Windows batch files by providing a fully functional programming language and the capability to natively run .Net code.  By leveraging the .Net Windows Forms DLLs, Powershell can rise to even greater heights, leaving the command prompt and giving users a graphical interface.

This example is primarily focused on instantiating the form, adding controls, connecting the events, and providing the return value.  With this framework, the GUI can be easily extended to incorporate drop-down controls, field validation, radio buttons, and the variety of other controls available in the .Net framework.

The sample code below creates a simple GUI for entering a URL:

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms");
 
function Get-URL {
  $Form = New-Object System.Windows.Forms.Form;
  $Form.width = 300
  $Form.height = 110
  $Form.Text = ”Please enter a URL”
 
  $txtURL = new-object System.Windows.Forms.TextBox;
  $txtURL.Location = new-object System.Drawing.Size(130,7)
  $txtURL.Size = new-object System.Drawing.Size(130,30)
  $Form.Controls.Add($txtURL);
 
  $lblURL = new-object System.Windows.Forms.Label;
  $lblURL.Location = new-object System.Drawing.Size(10,10)
  $lblURL.size = new-object System.Drawing.Size(130,20)
  $lblURL.Text = "Please enter a URL:"
  $Form.Controls.Add($lblURL);
 
  $btnSubmit = new-object System.Windows.Forms.Button
  $btnSubmit.Location = new-object System.Drawing.Size(100,40)
  $btnSubmit.Size = new-object System.Drawing.Size(100,20)
  $btnSubmit.Text = "Submit"
  $btnSubmit.Add_Click({Return-Get-URL});
  $form.Controls.Add($btnSubmit);
 
  $Form.Add_Shown({$Form.Activate()});
  $x = $Form.ShowDialog();
  return $txtURL.Text;
}
 
function Return-Get-URL {
  $Form.Close();
}
 
$url = Get-URL
echo $url

The form is encapsulated into a function to help simplify execution and provide a return value.  Executing the form and retrieving the input can be accomplished with one statement: “$url = Get-URL”.  The Get-URL function currently returns the text string, however more complicated forms with several inputs could store return values in an associative array or custom object.

The Get-URL function creates a new form using standard .Net syntax.  Alternatively, the form could be designed using a tool such as Visual Studio, and then converted to Powershell by adding a dollar design in front of every variable, and using the “new-object” statement to instantiate instances of a class.

A key element of the GUI input form is waiting for completion of the form before returning to program flow.  Since the form is run through the “ShowDialog” function, the command will block program execution until the form is closed.  Upon the Form Close event, values of the input elements can be extracted and assigned to the return value of the function.

While “ShowDialog” will block execution until the Form closes, there needs to be an actual event that closes the form.  This is accomplished by adding an event handler to btnSubmit, that executes the “Form.Close” function when the button is clicked.  In more elaborate instances, this function would perform validation and show error messages if the input was incorrect.

Implementing GUIs in Powershell helps bridge the gap between the utility of batch scripts and the usability of a graphical interface.  Users can extend Powershell to create makeshift applications, and allow non-technical users to  take advantage of back-end scripts that simplify repetitive tasks.

Written by Andrew Palczewski

About the Author
Andrew Palczewski is CEO of apHarmony, a Chicago software development company. He holds a Master's degree in Computer Engineering from the University of Illinois at Urbana-Champaign and has over ten years' experience in managing development of software projects.
Google+

RSS Twitter LinkedIn Facebook Email

One thought on “Creating a GUI with Powershell”

Leave a Reply to Steve Grossman Cancel reply

Your email address will not be published. Required fields are marked *