PS: Creating A GUI Application

3 minute read

Description:

Scripting is something I find myself really enjoying here lately. With sites like poshgui.com it’s common to want to create a custom form either for your use or to send to helpdesk users. Here is a post on a template you can use:

This will create a form like the screenshot below that will launch the form and keep it open after you make selections.

To Resolve:

  1. So just copy and paste this in your PS_ISE and add scriptblocks like in lines 10-29 and then assign them to click events like in lines 56,66,76, etc.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    
    Function Invoke-Form
    {
    Function Initialize-Window
    {
       $t = '[DllImport("user32.dll")] public static extern bool ShowWindow(int handle, int state);'
       Add-Type -Name Win -Member $t -Namespace native
       [native.win]::ShowWindow(([System.Diagnostics.Process]::GetCurrentProcess() | Get-Process).MainWindowHandle, 0)
    }
    Initialize-Window
    
    $StartPowershell = 
    { 
       Powershell.exe Start-Process Powershell -Verb runas 
    }
    
    $StartPowershellISE = 
    { 
       Powershell.exe Start-Process "Powershell_ise" -Verb runas 
    }
    
    $RunMyScript = 
    { 
       Start-Process Powershell -Argument "C:\scripts\resources\invokepassword.ps1"
    }
    
    $EndForm = 
    { 
       Stop-Process -id $pid
    }
    
    Add-Type -AssemblyName System.Windows.Forms
    $Form = New-Object system.Windows.Forms.Form 
    $Form.Text = "GerrysScripts"
    $Form.TopMost = $true
    $Form.BackColor = "#0b0b0b"
    $Form.Width = 256
    $Form.Height = 410
    
    $label = New-Object system.windows.Forms.Label 
    $label.Text = "Select A Task `r`nTo Run:"
    $label.AutoSize = $true
    $label.ForeColor = "#0CF110"
    $label.Width = 217
    $label.Height = 10
    $label.location = new-object system.drawing.point(5, 19)
    $label.Font = "Verdana,12"
    $Form.controls.Add($label) 
    
    $button = New-Object system.windows.Forms.Button 
    $button.Text = "StartPS"
    $button.ForeColor = "#0CF110"
    $button.Width = 159
    $button.Height = 23
    $button.location = new-object system.drawing.point(4, 70)
    $button.Font = "Verdana,10"
    $button.Add_Click($StartPowershell)
    $Form.controls.Add($button)
    
    $button2 = New-Object system.windows.Forms.Button 
    $button2.Text = "StartISE"
    $button2.ForeColor = "#0CF110"
    $button2.Width = 159
    $button2.Height = 23
    $button2.location = new-object system.drawing.point(4, 100)
    $button2.Font = "Verdana,10"
    $button2.Add_Click($StartPowershellISE)
    $Form.controls.Add($button2)
    
    $button3 = New-Object system.windows.Forms.Button 
    $button3.Text = "button"
    $button3.ForeColor = "#0CF110"
    $button3.Width = 159
    $button3.Height = 23
    $button3.location = new-object system.drawing.point(4, 130)
    $button3.Font = "Verdana,10"
    $button3.Add_Click($RunMyScript)
    $Form.controls.Add($button3) 
    
    $button4 = New-Object system.windows.Forms.Button 
    $button4.Text = "button"
    $button4.ForeColor = "#0CF110"
    $button4.Width = 159
    $button4.Height = 23
    $button4.location = new-object system.drawing.point(4, 160)
    $button4.Font = "Verdana,10"
    $button4.Add_Click($StartPowershellISE)
    $Form.controls.Add($button4)
    
    $button5 = New-Object system.windows.Forms.Button 
    $button5.Text = "button"
    $button5.ForeColor = "#0CF110"
    $button5.Width = 159
    $button5.Height = 23
    $button5.location = new-object system.drawing.point(4, 190)
    $button5.Font = "Verdana,10"
    $button5.Add_Click($StartPowershellISE)
    $Form.controls.Add($button5) 
    
    $button6 = New-Object system.windows.Forms.Button 
    $button6.Text = "button"
    $button6.ForeColor = "#0CF110"
    $button6.Width = 159
    $button6.Height = 23
    $button6.location = new-object system.drawing.point(4, 220)
    $button6.Font = "Verdana,10"
    $button6.Add_Click($StartPowershellISE)
    $Form.controls.Add($button6) 
    
    $button7 = New-Object system.windows.Forms.Button 
    $button7.Text = "button"
    $button7.ForeColor = "#0CF110"
    $button7.Width = 159
    $button7.Height = 23
    $button7.location = new-object system.drawing.point(4, 250)
    $button7.Font = "Verdana,10"
    $button7.Add_Click($StartPowershellISE)
    $Form.controls.Add($button7) 
    
    $button8 = New-Object system.windows.Forms.Button 
    $button8.Text = "button"
    $button8.ForeColor = "#0CF110"
    $button8.Width = 159
    $button8.Height = 23
    $button8.location = new-object system.drawing.point(4, 280)
    $button8.Font = "Verdana,10"
    $button8.Add_Click($StartPowershellISE)
    $Form.controls.Add($button8)
    
    $button9 = New-Object system.windows.Forms.Button 
    $button9.Text = "button"
    $button9.ForeColor = "#0CF110"
    $button9.Width = 159
    $button9.Height = 23
    $button9.location = new-object system.drawing.point(4, 310)
    $button9.Font = "Verdana,10"
    $button9.Add_Click($StartPowershellISE)
    $Form.controls.Add($button9)
    
    $button10 = New-Object system.windows.Forms.Button 
    $button10.Text = "Close Form"
    $button10.ForeColor = "#0CF110"
    $button10.Width = 159
    $button10.Height = 23
    $button10.location = new-object system.drawing.point(4, 340)
    $button10.Font = "Verdana,10"
    $button10.Add_Click($EndForm)
    $Form.controls.Add($button10)     
    
    $Form.ShowDialog() | out-null
    }
    }
    
    Invoke-Form
    
  2. Source is maintained under gwApplications

Comments