GUI Forms PowerShell

    GUI Forms PowerShell

    Windows PowerShell also supports building of Graphical User Interface (Forms). Many of the GUI administration tools available are scripted in PowerShell. There are many commercial and free tools available in the market, but let us see how to build a GUI form with PowerShell scripting.

    Let us build a simple form now.

    There are only three things required to launch a form from PowerShell:

    1. Load the System.Windows.Forms assembly;
    2. Create a new object of type system.windows.forms.form
    3. Call the ShowDialog() method on your new object.
    Add-Type -AssemblyName System.Windows.Forms
    $Form = New-Object system.Windows.Forms.Form
    $Form.ShowDialog()

    Save the above code and run the code. You can see a GUI Form like the following:

    Let us add some more features to our form.

    $Form.Height = 500
    $Form.Width = 500

    the above code will change the size of our form to 500 x 500.

    $Form.BackColor = "Red" ## this will change the background color of the form to Red.

    Now, let us add some more controls to our form.

    Let is add some more functionality to our form.

    Add-Type -AssemblyName System.Windows.Forms
    $Form = New-Object system.Windows.Forms.Form
    $Form.Height = 200
    $Form.Width = 400
     
    $Label1                          = New-Object system.Windows.Forms.Label
    $Label1.text                     = "Click the button to get OS details"
    $Label1.AutoSize                 = $true
    $Label1.width                    = 25
    $Label1.height                   = 10
    $Label1.location                 = New-Object System.Drawing.Point(20,40)
    $Label1.Font                     = 'Microsoft Sans Serif,10'
     
    $TextBox1                        = New-Object system.Windows.Forms.TextBox
    $TextBox1.multiline              = $false
    $TextBox1.width                  = 300
    $TextBox1.height                 = 20
    $TextBox1.location               = New-Object System.Drawing.Point(20,80)
    $TextBox1.Font                   = 'Microsoft Sans Serif,10'
     
    $Button1                         = New-Object system.Windows.Forms.Button
    $Button1.text                    = "Click me!"
    $Button1.width                   = 80
    $Button1.height                  = 30
    $Button1.location                = New-Object System.Drawing.Point(257,34)
    $Button1.Font                    = 'Microsoft Sans Serif,10'
     
    $Form.controls.AddRange(@($Label1,$TextBox1,$Button1))
     
    $Button1.Add_Click({ $TextBox1.Text = (Get-WmiObject win32_operatingsystem).Caption; })
     
    $Form.ShowDialog()

    Save and run the code with PowerShell. On clicking the button, the textbox field will be updated with the Operating System Name of the current machine.

    Let is break down the code:

    Create the form:

    Add-Type -AssemblyName System.Windows.Forms  ## adding the assembly
    $Form = New-Object system.Windows.Forms.Form  ## we are defining the form here
    $Form.Height = 200  ## Form heoght
    $Form.Width = 400  ## Form width

    Create Label control:

    $Label1 = New-Object system.Windows.Forms.Label  ## Create Label
    $Label1.text = "Click the button to get OS details"  ## Label text that will be displayed
    $Label1.width = 25  ## Label width
    $Label1.height = 10  ## Label height
    $Label1.location = New-Object System.Drawing.Point(20,40)  ## Label Position

    Create TextBox Control:

    $TextBox1 = New-Object system.Windows.Forms.TextBox  ## Create TextBox
    $TextBox1.multiline = $false  ## restricting the text to a single line. make this $true if you want to maek a multiline textbox and define number of lines.
    $TextBox1.width = 300   ## width
    $TextBox1.height = 20  ## height
    $TextBox1.location = New-Object System.Drawing.Point(20,80)  ## position

    Create Button Control:

    $Button1 = New-Object system.Windows.Forms.Button  ## create Button control
    $Button1.text = "Click me!"  ## Button Text that will be displayed
    $Button1.width = 80  ## Button width
    $Button1.height = 30  ## Button height
    $Button1.location = New-Object System.Drawing.Point(257,34)   ## Button Position

    Add Controls to the Form:

    $Form.controls.AddRange(@($Label1,$TextBox1,$Button1))  ## this line will add the controls created in previous steps.

    Add Button Click action:

    $Button1.Add_Click({ $TextBox1.Text = (Get-WmiObject win32_operatingsystem).Caption; })

    Show the Form:

    $Form.ShowDialog()

     

     

    Here are some of the GUI designer tools that you can try. I will add more in the future. You can try the following tools for building GUI utilities.

    POSHGUI

    Sapian PowerShell Studio

     


    © 2019 WinAdmin.org . All Rights Reserved.
    Cookies make it easier for us to provide you with our services. With the usage of our services you permit us to use cookies.
    Ok Decline