PowerShell – Form Input Field; GUI

email me

Want to add some visual components to your PowerShell scripts? Maybe accept user input? Or, display a message? These GUI elements can be created using PowerShell.

In this post, I create a simple form, center the form on the screen, create an input field, and perform some logic on the input.


What the form looks like


The pop up when the user enters some data


The pop up when the user does not enter any data



Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# Create form and center
$Form1 = New-Object System.Windows.Forms.Form
$Form1.Text = "Data Form"
$Form1.Size = New-Object System.Drawing.Size(300,200)
$Form1.StartPosition = "CenterScreen"
# Icon
$Form1.Icon = [Drawing.Icon]::ExtractAssociatedIcon((Get-Command powershell).Path)

# Create the buttons
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Form1.AcceptButton = $OKButton

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$Form1.CancelButton = $CancelButton

# Create a text label for input box
$Label1 = New-Object System.Windows.Forms.Label
$Label1.Location = New-Object System.Drawing.Point(10,20)
$Label1.Size = New-Object System.Drawing.Size(280,20)
$Label1.Text = "Enter data here:"

# Create an input box
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)

$Form1.Topmost = $True

$result = $Form1.ShowDialog()

# Perform logic
# When user clicks OK
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
            $x = $textBox.Text

        if ($x) {

            [System.Windows.Forms.MessageBox]::Show("You entered: $x", "Test Title",0,64)
            # YOUR CODE GOES HERE   
        else {

            [System.Windows.Forms.MessageBox]::Show("You didn't enter anything!", "Test Title",0,48)
            # YOUR CODE GOES HERE




Button Type

0  OK
1  OKCancel
2  AbortRetryIgnore
3  YesNoCancel
4  YesNo
5  RetryCancel


Dialog Box Icon

0    None
16  Hand
16  Error
16  Stop
32  Question
48  Exclamation
48  Warning
64  Asterisk
64  Information


Suppress Error

Command -ErrorAction SilentlyContinue


Suppress all Errors

$ErrorActionPreference= ‘silentlycontinue’


More advanced form


← Previous  Intune – PowerShell – Add Screensaver Config


tags: MrNetTek, Eddie Jackson, Computer, Automation, “Merit is Might”, Artificial Intelligence, “Who is MrNetTek”

30 years of job experience! IT Professional. Computers, Electronics, Solar. ACM Member.