Create GUIs for Powershell scripts? Yeah, sometimes that could be useful…if you want to create a tool to distribute to your team without requiring them to remember switches or reviewing what the script does (especially if not used much).
Below is a template I worked up to speed up the process of slapping a GUI on a script.
Here’s the Powershell template:
function NewValue() { # $textBox1.text.ToString() } function Option1() { #do stuff } function Option2() { #do stuff } function OptionA() { #do stuff } function OptionB() { #do stuff } Add-Type -AssemblyName System.Windows.Forms $Form = New-Object system.Windows.Forms.Form $Form.Text = "Form" $Form.TopMost = $true $Form.Width = 435 $Form.Height = 300 #$Form.BackColor = "LightGray" $checkBox1 = New-Object system.windows.Forms.CheckBox $checkBox1.Text = "EnterValue" $checkBox1.AutoSize = $true $checkBox1.Width = 95 $checkBox1.Height = 20 $checkBox1.location = new-object system.drawing.point(20,22) $checkBox1.Font = "Microsoft Sans Serif,12" $checkBox1.CheckState = "Checked" $Form.controls.Add($checkBox1) $textBox1 = New-Object system.windows.Forms.TextBox $textBox1.Width = 200 $textBox1.Height = 20 $textBox1.location = new-object system.drawing.point(200,20) $textBox1.Font = "Microsoft Sans Serif,12" $Form.controls.Add($textBox1) $checkBox2 = New-Object system.windows.Forms.CheckBox $checkBox2.Text = "Option1" $checkBox2.AutoSize = $true $checkBox2.Width = 95 $checkBox2.Height = 20 $checkBox2.location = new-object system.drawing.point(20,125) $checkBox2.Font = "Microsoft Sans Serif,12" $checkBox2.CheckState = "Checked" $Form.controls.Add($checkBox2) $checkBox3 = New-Object system.windows.Forms.CheckBox $checkBox3.Text = "Option2" $checkBox3.AutoSize = $true $checkBox3.Width = 95 $checkBox3.Height = 20 $checkBox3.location = new-object system.drawing.point(20,160) $checkBox3.Font = "Microsoft Sans Serif,12" $checkBox3.CheckState = "Checked" $Form.controls.Add($checkBox3) $radioButton1 = New-Object system.windows.Forms.RadioButton $radioButton1.Text = "OptionA" $radioButton1.AutoSize = $true $radioButton1.Width = 104 $radioButton1.Height = 20 $radioButton1.location = new-object system.drawing.point(40,75) $radioButton1.Font = "Microsoft Sans Serif,12" $Form.controls.Add($radioButton1) $radioButton2 = New-Object system.windows.Forms.RadioButton $radioButton2.Text = "OptionB" $radioButton2.AutoSize = $true $radioButton2.Width = 104 $radioButton2.Height = 20 $radioButton2.location = new-object system.drawing.point(140,75) $radioButton2.Font = "Microsoft Sans Serif,12" $Form.controls.Add($radioButton2) # Create a group that will contain your radio buttons $GroupBox = New-Object System.Windows.Forms.GroupBox $GroupBox.Location = '20,60' $GroupBox.size = '210,50' $GroupBox.text = "Container" $Form.controls.Add($GroupBox) $button1 = New-Object system.windows.Forms.Button $button1.Text = "Go!" $button1.Width = 60 $button1.Height = 30 $button1.location = new-object system.drawing.point(125,220) $button1.Font = "Microsoft Sans Serif,12" $Form.controls.Add($button1) $button1.Add_Click( { if($checkBox1.CheckState -eq "Checked"){ EnterValue } if ($radioButton1.checked){ OptionA } if ($radioButton2.checked){ OptionB } if($checkBox2.CheckState -eq "Checked"){ Option1 } if($checkBox3.CheckState -eq "Checked"){ Option2 } } ) $button2 = New-Object system.windows.Forms.Button $button2.Text = "Exit" $button2.Width = 60 $button2.Height = 30 $button2.location = new-object system.drawing.point(200,220) $button2.Font = "Microsoft Sans Serif,12" $Form.controls.Add($button2) $button2.Add_Click( { $Form.Close() } ) [void]$Form.ShowDialog() $Form.Dispose()