PowerShell and Windows Forms

I’ve been getting questions about how to use PowerShell and windows forms for a while from different customers and friends, so I’ve decided to write a series of posts covering the basics and show what you can do with this.

So in this post I will show you the basics on how to create a form and add two buttons to it and some text. (Complete code at the end)

Part 1:

To start with, we need to load two assemblys;

  • System Windows Forms
  • System Drawing

We do this by adding these two lines

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

Next we need to create our main form, the window that will show when the script is run:

$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text = "First Form"
$MainForm.Size = New-Object System.Drawing.Size(800,600)
$MainForm.KeyPreview = $True
$MainForm.FormBorderStyle = "1"
$MainForm.MaximizeBox = $false
$MainForm.StartPosition = "CenterScreen"

Starting from the top in the code above, we create a new Windows Form called $MainForm and name the application “First Form”

The size of the windows should be 800*600 and we should accept input from the keyboard, such as “Enter” and “Tab”.

“BorderStyle” is a value between 1 and 7, I always use 1, but experiment to find what looks best in your eyes!

We also make sure that the user can’t maximize the window and that the application starts in the center of the screen.

Next we need to activate the form when we run the script. This is done by adding the following:

$MainForm.Add_Shown({$MainForm.Activate()})
[void] $MainForm.ShowDialog()

If we run the complete code that should look like the one below, this is the result we get:

MainForm

 

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

### Add Forms ###
$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text = "First Form"
$MainForm.Size = New-Object System.Drawing.Size(800,600)
$MainForm.KeyPreview = $True
$MainForm.FormBorderStyle = "1"
$MainForm.MaximizeBox = $false
$MainForm.StartPosition = "CenterScreen"

### Activate Form
$MainForm.Add_Shown({$MainForm.Activate()})
[void] $MainForm.ShowDialog()

Part 2:

The code above is a good start, but there’s not really anything we can do, more than showing a box. In this part we will resize the window, add a button and some text.

Let’s start with changing the size of the window to 400*200 by changing the line to:

$MainForm.Size = New-Object System.Drawing.Size(400,200)

Next, we need to add a button. To do this we add this code after we created to form, but before we activate it:

$Close = New-Object System.Windows.Forms.Button
$Close.Size = New-Object System.Drawing.Size(75,25)
$Close.Location = New-Object System.Drawing.Size(165,110)
$Close.Text = "Close"
$MainForm.Controls.Add($Close)
$Close.add_click({[void] $MainForm.Close()})

This will add a button called “Close” to our form with the size of 75*25 on the coordinates 165,110. When we press the button it will close the form by running:

[void] $MainForm.Close()

(You can also call a function by adding the name of the function between ({ <function> }))

Go ahead and run the script and try it out, the window should look like this:

Button

Part 3:

The last part in this post will show you how to add some text as well, a window with a button isn’t that helpful unless you know what it does.

After the code that contains the button, we add this:

$Text = New-Object System.Windows.Forms.Label
$Text.Size = New-Object System.Drawing.Size(300,50)
$Text.Location = New-Object System.Drawing.Size(70,50)
$Text.Text = "Press the button to close this window"
$MainForm.Controls.Add($Text)

Now when you run the script, it will look like the following:

Text
Stay tuned for more posts on how to use Windows Forms with PowerShell!

The complete code:

[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

### Add Forms ###
$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text = "First Form"
$MainForm.Size = New-Object System.Drawing.Size(400,200)
$MainForm.KeyPreview = $True
$MainForm.FormBorderStyle = "1"
$MainForm.MaximizeBox = $false
$MainForm.StartPosition = "CenterScreen"

### Add Buttons ###
$Close = New-Object System.Windows.Forms.Button
$Close.Size = New-Object System.Drawing.Size(75,25)
$Close.Location = New-Object System.Drawing.Size(165,110)
$Close.Text = "Close"
$MainForm.Controls.Add($Close)
$Close.add_click({[void] $MainForm.Close()})

### Add Lables ###
$Text = New-Object System.Windows.Forms.Label
$Text.Size = New-Object System.Drawing.Size(300,50)
$Text.Location = New-Object System.Drawing.Size(70,50)
$Text.Text = "Press the button to close this window"
$MainForm.Controls.Add($Text)

### Activate Form
$MainForm.Add_Shown({$MainForm.Activate()})
[void] $MainForm.ShowDialog()

 

This entry was posted in Powershell and tagged , , . Bookmark the permalink.

Leave a Reply

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