PowerShell with Windows Forms

On a Friday night, after my work, I was wondering (finally some self-daydream) is there a way that PowerShell can create application-like. All I needed to get the theory and functionality working, beauty is later.

I texted my buddy in Singapore (.Net Dev) first then Europe (PowerShell master) and asked them, both of them gave me different platform answers but a similar answer was using Windows Form. The surprising thing was PowerShell can do it, I was like “Hmm…a platform the I am familiar with, let’s give that a try.”. Visual Studio also has Windows Form but I prefer to start off the platform that I familiarize first, you know, give myself a base to kick-start. Well, that does not mean that I won’t give Visual Studio a try.
Thanks very much for these 2 Masters!

So this is why I am writing this post, after gathering enough information from these 2 buddies (Masters), I start my journey towards Windows Form. Well, for a beginner is usually start off with “Hello world” statement. I try out the first reference link, this is what I get the outcome;

  • A simple pop out of “Hello World with an OK button”
  • Behavior: A Message pop up and selecting OK will returning a result

ps01.PNG

A simple pop up gets me excited to explore more of this Windows Form namespace.

So to gain further understanding, I tried to type something else, I am trying to get a behavior where I click on the “Good Luck!” button then a cute cat or Pikachu meme will pop out, not sure whether it is possible. Oh well, more exploring to do.

ps02.PNG

#A form with a simple display at the Center of the Screen and a button
Add-Type -AssemblyName System.Windows.Forms
$Form = New-Object System.Windows.Forms.Form
$Form.StartPosition = 'CenterScreen'

#Attributes or Controls for your form
$Form.Text = "Windows Form"
$Label.Text = "Sabrina's first Windows Form"
$GoodluckButton = New-Object System.Windows.Forms.Button
$GoodluckButton.Text = 'Good Luck!'
$GoodluckButton.Location = New-Object System.Drawing.Point(100,120)
$GoodluckButton.DialogResult = [System.Windows.Forms.DialogResult]::OK

#What are the controls to be added into the form
$Form.Controls.Add($Label)
$Form.Controls.Add($GoodluckButton)

#The result
$Form.ShowDialog()

References:

  1. https://www.powershellmagazine.com/2015/04/09/pstip-use-windows-forms-to-generate-gui-messagebox/

Author: sabrinaksy

Just an ordinary lady who love what she does best.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: