In this tutorial, I show you the basics of creating a graphical user interface (GUI) using Windows PowerShell
The framework of libraries .NET also referred to as assemblies, are provided specifically for graphical user interface creation, web application development, business process handling, and database access, and more…
WinForms is a subset of the .NET framework designed specifically for the creation of Windows-based GUIs.
To start Windows PowerShell from the Start menu, click Start, write PowerShell, and will see something like:
The next image shows us the ways to write source code, command line and Windows PowerShell Integrated Scripting Environment (ISE).
Let’s try one example. I will use a source from the last tutorial about reading BIOS info.
This is the source code:
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 | # Load the Winforms assembly [reflection.assembly]::LoadWithPartialName( "System.Windows.Forms") # Create the windows orm $form = New-Object Windows.Forms.Form #Set the dialog title $form.text = "PowerShell WinForms" # Create the labels control and set text, size and location $label = New-Object Windows.Forms.Label $label.Location = New-Object Drawing.Point 50,30 $label.Size = New-Object Drawing.Point 200,15 $label.text = "Name" $label1 = New-Object Windows.Forms.Label $label1.Location = New-Object Drawing.Point 50,60 $label1.Size = New-Object Drawing.Point 200,15 $label1.text = "Version" $label2 = New-Object Windows.Forms.Label $label2.Location = New-Object Drawing.Point 50,90 $label2.Size = New-Object Drawing.Point 200,15 $label2.text = "Manufacturer" $label3 = New-Object Windows.Forms.Label $label3.Location = New-Object Drawing.Point 50,120 $label3.Size = New-Object Drawing.Point 200,15 $label3.text = "SMBIOSBIOS Version" # Create Button and set text , location and size $button = New-Object Windows.Forms.Button $button.text = "Click to read BIOS !" $button.Location = New-Object Drawing.Point 50,150 $button.Size = New-Object Drawing.Point 200,30 # Read data from BIOS [string]$strComputer = "." $colItems = get-wmiobject -class "Win32_BIOS" -namespace "root\CIMV2" -computername $strComputer foreach ($objItem in $colItems) { write-host "Name :" $objItem.Name write-host "Version :" $objItem.Version write-host "Manufacturer :" $objItem.Manufacturer write-host "SMBIOSBIOS Version :" $objItem.SMBIOSBIOSVersion write-host } # Set up event handler to show data BIOS $button.add_click({ $label.Text = "Name BIOS: " + $objItem.Name $label1.Text = "Version : " + $objItem.Version $label2.Text = "Manufacturer: " + $objItem.Manufacturer $label3.Text = "SMBIOSBIOS Version:" + $objItem.SMBIOSBIOSVersion }) # Add the controls to the Form $form.controls.add($button) $form.controls.add($label) $form.controls.add($label1) $form.controls.add($label2) $form.controls.add($label3) # Display the Form $form.ShowDialog() |
Run this script with PowerShell or with Windows PowerShell Integrated Scripting Environment (ISE).
Now that we have seen a simple GUI created in Windows PowerShell.