Learning and Sharing
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
  • Home
  • Blog
  • Linux
  • macOS
  • Virtualization
    • VMware
    • VirtualBox
  • Windows
    • Windows 11
    • Windows 10
    • Windows Server
  • Series
    • Symantec
    • Intune
    • Microsoft Azure
    • Powershell
    • VirtualBox
    • VMware
    • PowerShell Learning
    • Microsoft Graph
  • More
    • Auto Installation
    • AEC Installation
  • Contact
No Result
View All Result
No Result
View All Result

What is the WinForms in PowerShell?

December 18, 2023
in Blog, Powershell, PowerShell WinForms
0
ADVERTISEMENT

Table of Contents

PowerShell is an advanced shell with integration of .NET objects. It’s more than just a replacement for the older cmd.exe. It can work with .NET assemblies, process large datasets, and even interact with web services.

Because of the .NET assemblies support, it can work with WinForms (or even WPF), making it possible to create scripts with GUIs.

System requirements

This has been tested to work with Windows PowerShell version 5.1. It’s likely going to work with older versions as well. But, because it depends on the .Net framework, so it’s not going to work with the new cross-platform PowerShell (there are no WinForms on Linux/macOS). You can check the version with

PS C:\> Get-Host | Select-Object version

Version
-------
5.1.22621.2506

PS C:\> Get-Host | Select-Object version

Version
-------
7.3.10

Setting up the environment

Before we can start, let’s check a few things.

The first one is the script execution policy. It controls which scripts can be run. By default, Windows blocks execution of all scripts (more on that here).

We have to allow it to run local scripts that are not digitally signed. It’s possible to do this either by going through:

  • Windows 10: Windows Settings > Updates & Security > For developers > Change execution policy.
  • Windows 11: Windows Settings > For developers > PowerShell > Change execution policy.

Or just executing from administrator PowerShell.

Set-ExecutionPolicy RemoteSigned

Another (less important) thing is the code editor. Even though we could just write the entire script directly in PowerShell, it’s easier to use a full-featured editor with error-checking and syntax highlighting. Windows already comes with PowerShell ISE (Integrated Scripting Environment), but you can use Visual Studio Code with the PowerShell extension.

Writing a PowerShell script

Importing the assemblies

We have to import both System.Windows.Forms and System.Drawing assemblies. It’s possible to only include the first one but we also need the 2nd to specify control sizes.

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

You can test it by creating a blank form:

Add-Type -AssemblyName System.Windows.Forms, System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.ShowDialog()
V9n7GG7T5ymND9KOH74ybHssA6Q2BlWzOGmObRKkddZLhQTbjg4TNfs1Wyle

Adding controls

Let’s make a “Hello World” form. First, we create a top-level Form object:

Add-Type -AssemblyName System.Windows.Forms, System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.ShowDialog()

$form           = New-Object System.Windows.Forms.Form
$form.Text      = "Some form"
$form.Size      = New-Object System.Drawing.Size(150, 145)
$form.AutoSize  = $true

In PowerShell, objects are created using New-Object. You can also pass parameters to constructors, similar to the new keyword in C#. Values are assigned to properties directly. Another difference is using $true instead of just true.

Let’s add a label and a button:

$label1          = New-Object System.Windows.Forms.Label
$label1.Text     = "Hello World!"
$label1.Location = New-Object System.Drawing.Point(30, 20);

$button                 = New-Object System.Windows.Forms.Button
$button.Text            = "Close"
$button.location        = New-Object System.Drawing.Point(30, 60);
$button.DialogResult    = [System.Windows.Forms.DialogResult]::OK

The $button.DialogResult line tells the form what to return when the button is clicked. You can use this to figure out whether the user clicked OK or Cancel. We also make $btn the default button and lay controls onto the form:

$form.AcceptButton = $button
$form.controls.Add($label1)
$form.controls.Add($button)

All that’s left is showing the form itself:

$form.ShowDialog()

Bring all parts together:

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

$form           = New-Object System.Windows.Forms.Form
$form.Text      = "Some form"
$form.Size      = New-Object System.Drawing.Size(150, 145)
$form.AutoSize  = $true

$label1          = New-Object System.Windows.Forms.Label
$label1.Text     = "Hello World!"
$label1.Location = New-Object System.Drawing.Point(30, 20);

$button                 = New-Object System.Windows.Forms.Button
$button.Text            = "Close"
$button.location        = New-Object System.Drawing.Point(30, 60);
$button.DialogResult    = [System.Windows.Forms.DialogResult]::OK

$form.AcceptButton = $button
$form.controls.Add($label1)
$form.controls.Add($button)

$form.ShowDialog()
Gxv9ftrfv42345rwertwecsessdasvt9fdczczxcsfdsdfwU6JME8I3dsdfdsaofGF65756WSr5345365fsd

Event handlers

In our form, $button is the default OK button which just terminates the form. But we can use non-terminating event handlers as well. For example, let’s make it possible to click on the label:

$lable1.Add_Click({
    [System.Windows.Forms.MessageBox]::Show("This is a lable!")
})

You can call functions or scriptblocks from event handlers as you normally would.

Visual Styles

Sometimes, I’ve noticed with these GUI scripts is that different control styles are used when the script is run from the PowerShell console instead of VSCode. The console uses legacy rendering which falls back to using Windows 95-style controls. We need to enable Visual Styles to fix that:

[System.Windows.Forms.Application]::EnableVisualStyles()

From here on, you can add more controls and event handlers.

  • TextBox
  • Label
  • Button
  • PictureBox
  • CheckBox
  • ComboBox (Dropdown list)
  • ListBox
  • RadioButton
  • Groupbox
  • ProgressBar
  • DataGridView
  • ColorDialog

The full script with GUI:

Add-Type -AssemblyName System.Windows.Forms, System.Drawing
[System.Windows.Forms.Application]::EnableVisualStyles()

$form           = New-Object System.Windows.Forms.Form
$form.Text      = "Some form"
$form.Size      = New-Object System.Drawing.Size(150, 145)
$form.AutoSize  = $true

$label1          = New-Object System.Windows.Forms.Label
$label1.Text     = "Hello World!"
$label1.Location = New-Object System.Drawing.Point(30, 20);

$button                 = New-Object System.Windows.Forms.Button
$button.Text            = "Close"
$button.location        = New-Object System.Drawing.Point(30, 60);
$button.DialogResult    = [System.Windows.Forms.DialogResult]::OK

$form.AcceptButton = $button
$form.controls.Add($label1)
$form.controls.Add($button)

$form.ShowDialog()
5/5 - (1 vote)
Previous Post

[Winforms] Creating a Responsive Windows Form Using PowerShell With Runspaces

Next Post

Find the Creation Date of a Microsoft 365 Tenant

Related Posts

Running Hyper-V and VMware Workstation on The Same Machine

August 15, 2024

How to Uninstall All Autodesk Products At Once Silently

July 29, 2024
Ftr5

How to Uninstall the Autodesk Genuine Service on Windows

July 29, 2024
Ftr19

How to Fix Windows Cannot Read the ProductKey From the Unattend Answer File in VirtualBox

July 26, 2024
Ftr25

How to Update Windows Terminal in Windows 10/11

July 26, 2024

How to Disable The Beep Sound in WSL Terminal on Windows

July 26, 2024

Leave a Reply Cancel reply

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

Recent Posts

  • How To Turn On uBlock Origin Extension in Chrome (2025)
  • Images Hidden Due To Mature Content Settings In CivitAI
  • Azure OpenAI vs Azure AI Hub, How to Choose the Right One for Your Needs

Categories

Stay in Touch

Discord Server

Join the Discord server with the site members for all questions and discussions.

Telegram Community

Jump in Telegram server. Ask questions and discuss everything with the site members.

Youtube Channel

Watch more videos, learning and sharing with Leo ❤❤❤. Sharing to be better.

Newsletter

Join the movement and receive our weekly Tech related newsletter. It’s Free.

General

Microsoft Windows

Microsoft Office

VMware

VirtualBox

Technology

PowerShell

Microsoft 365

Microsoft Teams

Email Servers

Copyright 2025 © All rights Reserved. Design by Leo with ❤

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory

No Result
View All Result
  • Home
  • Linux
  • Intune
  • macOS
  • VMware
  • VirtualBox
  • Powershell
  • Windows 10
  • Windows 11
  • Microsoft 365
  • Microsoft Azure
  • Microsoft Office
  • Active Directory