In the PowerShell tool djaus2/az-iothub-ps on GitHub, I added some graphical UI forms. The UI was first designed in a Windows Forms C# app. This post tells you how.

PowerShell under the hood is .NET. You can invoke .NET APIs directly in PS scripts. You CAN also invoke Windows Forms with some mechanical changes to the Winodws Form designer form. This post documents an example of porting the Form1.Designer.cs file to a PowerShell script.

There are tools for turning a PowerShell script into an .EXE. This port could be construed as step in that direction. Please leave comments below if there is better was of doing this.

Finally, before getting start, I can envisage a tool that parsed a Form.Designer.cs file and automatically ports it to .ps1. … Later.

The PowerShell tool is available on GitHub at djaus2/az-iothub-ps
The end product of this port, show-form.ps1 is here.
Another PS WinFom PS, show-image.ps1, is here. This displays an image.
And another, a variant of show-image.ps1, is here. This displays a sequence of images like PPT.

The project ported here is in GitHub here.
The actual file ported to PowerShell, Form1.Designer.cs can be view here in that project.

Steps for porting a Windows.Forms Form to PowerShell

Start by opening the Form1.Designer.cs file in Visual Studio.


In Visual Studio the region Windows Form Designer generated code is collapsed.

  1. Copy just that section into a new.ps1 file
  2. Add the function header and opening brace eg. function $show-form{
  3. As the first 2 lines of code below the opening brace, insert:
     Add-Type -AssemblyName System.Windows.Forms
     $Form = New-Object system.Windows.Forms.Form
  4. Add the closing brace at bottom

At the bottom

  1. Change this.ControlsAdd –> $Form.Controls.Add
  2. Similarly for all other this. in that block change to $Form.

Throughout rest of code:

  1. Remove all ;
  2. Change this.ControlName to $ControlName
    … where ControlName is the various names of controls on the form.
    Hint: Can global change this. to $ at this point
  3. Replace new with New-Object
  4. Remove lines with the following control properties


Identify common fonts used and instatiate a font object foir each. Reuse those throughout. Enables simple changes


Don’t create any objects inside object instatiation.

  • Separately instantiate them first then use them in the second object’s instantiation.

Event Handlers

Change for example:
this.button1.Click += new System.EventHandler(this.button1_Click); to:
$button1.Add_Click ({ What ever code })
For example:


And implement the function do-whatever


Where lists are added eg AddRange for example with Menus, create a range in PowerShell syntax and add that. For example:

  this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {

Create the list thus:
[System.Windows.Forms.ToolStripItem[]]$items = @($actionsToolStripMenuItem,$editToolStripMenuItem)
The datatype may not be required (??)
Then use thus:

  Next: > Blazor Gym Booking App
<  Prev:   az-iothub-ps
 This Category Links 
Category:Coding Index:Coding
  Next: > Nuget Packages 101
<  Prev:   PowerShell