PowerShell has full access to the .NET Framework and can therefore utilize all the graphical functions available in .NET. However, even though .NET Core is cross-platform, all graphical APIs are currently limited to the Windows operating system. If you choose this approach, your script will be restricted to Windows.
There are two available techniques: Windows Forms (older) and WPF (Windows Presentation Foundation, which is slightly newer).
In either case, all graphical elements—such as dialog windows, buttons, menu items, radio buttons, etc.—are represented as objects. Composing even a small UI can bloat your script and potentially add hundreds of lines of code.
Generating UIs with Windows Forms always follows these steps:
- Load the required .NET assemblies
- Create a blank window (System.Windows.Forms.Form)
- Create the required UI elements
- Position the UI elements on the window
- Configure any window properties or events you want to use
- Show the window
- Evaluate the user input after the window has been closed
To illustrate these steps, here is a simple Windows Forms-based UI that prompts the user to enter a username for logon:
# load the required .NET assemblies (requires Windows operating system)
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
# define content of user interface
$WindowTitle = 'Enter User Name'
$OkText = 'Log On'
$CancelText = 'Abort'
$Prompt = 'Please enter user name:'
$DefaultValue = $env:USERDOMAIN + '\' + $env:USERNAME
# create blank window
$form = [System.Windows.Forms.Form]::new()
$form.Text = $WindowTitle
$form.Size = '500,300'
$form.StartPosition = 'CenterScreen'
# create OK button
$OKButton = [System.Windows.Forms.Button]::new()
$OKButton.Location = '20,110'
$OKButton.Size = '120,35'
$OKButton.Text = $OkText
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
# create CANCEL button
$CancelButton = [System.Windows.Forms.Button]::new()
$CancelButton.Location = '150,110'
$CancelButton.Size = '120,35'
$CancelButton.Text = $CancelText
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
# create a label
$label = [System.Windows.Forms.Label]::new()
$label.Location = '20,35'
$label.Size = '625,30'
$label.Text = $Prompt
# create textbox to enter a text
$textBox = [System.Windows.Forms.TextBox]::new()
$textBox.Location = '20,70'
$textBox.Size = '350,30'
$textBox.Text = $DefaultValue
# add and position UI elements inside dialog:
$form.Controls.Add($OKButton)
$form.Controls.Add($CancelButton)
$form.Controls.Add($label)
$form.Controls.Add($textBox)
# configure window
# make it top-most
$form.Topmost = $true
# define its "accept"- and "cancel" buttons
$form.AcceptButton = $OKButton
$form.CancelButton = $CancelButton
# tie actions to events
# when the form shows, select the textbox
$form.Add_Shown({$textBox.Select()})
# show dialog in a modal dialog window
$result = $form.ShowDialog()
# evaluate results
$hasCanceled = $result -ne [System.Windows.Forms.DialogResult]::OK
if ($hasCanceled)
{
throw 'User canceled dialog'
}
# read textbox content
$textBox.Text
When you run this example, you’ll quickly notice both its advantages and drawbacks:
- The code is relatively simple, but each additional UI element adds many lines
- It works well in a PowerShell console, but may not scale correctly in graphical editors like the ISE, especially on high-resolution displays
The most cumbersome part of working with Windows Forms is designing the user interface. However, once you master this, you can create even the most complex interfaces—because essentially, you're leveraging the same resources available to professional application developers.
There are free tools available to help design Forms-based UIs, such as this designer:
PS C:\> Install-Module -Name powershell-designer -Scope CurrentUser
Once the module is installed, you can launch the designer using the following command:
PS C:\> powershell-designer
As you’ll quickly discover, designers like PowerShell-Designer are quite capable but also complex and not particularly intuitive.
In conclusion, Forms-based user interfaces can be a good option when your scripts run exclusively on Windows and require a highly specific UI.
Your ultimate PowerShell Cheat Sheet
Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.The poster is available for download and in paper form.
Related links
- ScriptRunners Script libary provides ready-to-use PowerShell scripts.
- Try out ScriptRunner here
- ScriptRunner: Book a demo with our product experts