PowerShell Form; GUI

email me

For all those that would like to extend their PowerShell scripting beyond the console and beyond just text-based scripts, you can add GUI components, such as things like buttons, drop down lists, radio buttons, etc. And, hey, no expensive software necessary; I did this in Notepad++.

In the example below, I have created a Form using only PowerShell, with an action behind the button.

What the form looks like

What the pop up looks like

 

The Code


function CreateForm {

# Import Assemblies
[reflection.assembly]::loadwithpartialname("System.Windows.Forms") | Out-Null
[reflection.assembly]::loadwithpartialname("System.Drawing") | Out-Null

$Form1 = New-Object System.Windows.Forms.Form
$Button1 = New-Object System.Windows.Forms.Button
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

$handler_Button1_Click=
{
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show("Pop up." , "Test Title")

# Example with Yes and No
# [System.Windows.Forms.MessageBox]::Show("We are proceeding with next step." , "Status" , 4)
# $OUTPUT= [System.Windows.Forms.MessageBox]::Show("We are proceeding with next step." , "Status" , 4)
# if ($OUTPUT -eq "YES" )
# {
# ..do something
#
# }
# else
# {
# ..do something else
# }

# Types of message boxes
# 0: OK
# 1: OK Cancel
# 2: Abort Retry Ignore
# 3: Yes No Cancel
# 4: Yes No
# 5: Retry Cancel
}

$OnLoadForm_StateCorrection=
{
$Form1.WindowState = $InitialFormWindowState
}
# Form Code
$Form1.Text = "Test Form Text 1"
$Form1.Name = "Test Form Text 2"

$Form1.MaximizeBox = $false #lock form
$Form1.FormBorderStyle = 'Fixed3D'
# None,FixedDialog,FixedSingle,FixedToolWindow,Sizable,SizableToolWindow

# Icon
$Form1.Icon = [Drawing.Icon]::ExtractAssociatedIcon((Get-Command powershell).Path)
#$NotifyIcon.Icon = [Drawing.Icon]::ExtractAssociatedIcon((Get-Command powershell).Path)

$Form1.DataBindings.DefaultDataSourceUpdateMode = 0
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 265
$System_Drawing_Size.Height = 55
$Form1.ClientSize = $System_Drawing_Size

$Button1.TabIndex = 0
$Button1.Name = "Button1"
$System_Drawing_Size = New-Object System.Drawing.Size
$System_Drawing_Size.Width = 240
$System_Drawing_Size.Height = 23
$Button1.Size = $System_Drawing_Size
$Button1.UseVisualStyleBackColor = $True

$Button1.Text = "Button Text Here"

$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 13
$System_Drawing_Point.Y = 13
$Button1.Location = $System_Drawing_Point
$Button1.DataBindings.DefaultDataSourceUpdateMode = 0
$Button1.add_Click($handler_Button1_Click)

$Form1.Controls.Add($Button1)

$InitialFormWindowState = $Form1.WindowState
$Form1.add_Load($OnLoadForm_StateCorrection)

# Show Form
$Form1.ShowDialog()| Out-Null

}

# Call Function
CreateForm

References

https://msdn.microsoft.com/en-us/library/system.windows.forms.form.formborderstyle%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396

https://msdn.microsoft.com/en-us/library/hw8kes41(v=vs.110).aspx