PowerShell GUI Basics

I recently had the need to write a very simple GUI app which would display some information. Digging into the world of creating GUIs with PowerShell I thought it would be a good idea to make some very simple code notes for future reference.

These are the absolute basics to get started.

Add-Type -assembly System.Windows.Forms

$Form = New-Object System.Windows.Forms.Form
$Form.Text = 'Title of the form window!'
$Form.Width = 1 # Settings this to 1 allows AutoSize to determine the minumum window size
$Form.Height = 1 # Settings this to 1 allows AutoSize to determine the minumum window size
$Form.AutoSize = $true 
$Form.FormBorderStyle = 'Fixed3D' # The user cannot resize the GUI window
$Form.MaximizeBox = $false  # The user cannot maximize the GUI window
$Form.TopMost = $true # The GUI window stays on top of other Windows

$Label = New-Object System.Windows.Forms.Label
$Label.Text = "Example label"
$Label.Font = 'Segoe UI, 20pt, style=Bold'
$label.TextAlign = "MiddleCenter"
$Label.AutoSize = $true

$Form.Controls.Add($Label)

$Form.ShowDialog() # This shows the GUI and starts an event loop inside the GUI.

The sapien.com blog has an excellent resource on the Label control.


If you enjoyed this post consider sharing it on , , , or , and .