The tutorial for today is about the PowerShell G.U.I. (GUI is defined as an acronym for graphical user interface).
You can solve this issue with an editor tool like POSHGUI, or use any text editor.
I will show you one commented script about how to create a form with a text and two buttons.
In this PowerShell script, you will see changes for each G.U.I. elements.
First, let’s see my version of PowerShell using the command prompt:
1 2 3 4 5 6 7 | powershell ... [System.Environment]::OSVersion.Version Major Minor Build Revision ----- ----- ----- -------- 10 0 18363 0 |
If you want to run scripts open the command prompt with Run as administrator privilege and start the PowerShell and run this commands.
1 2 3 | powershell ... Set-ExecutionPolicy unrestricted |
The system will prompt to confirm the change.
Enter the letter Y or press the enter key to change the execution policy setting.
After you run the script you can change it back with this command:
1 | Set-ExecutionPolicy AllSigned |
You have at this moment two PowerShell windows.
In the first one create your script with notepad and give the name GUI_001.ps1.
1 | notepad GUI_001.ps1 |
Add these source code to the file and run it with .\GUI_001.ps1:
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 | # Init PowerShell Gui Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing [System.Windows.Forms.Application]::EnableVisualStyles() # Create a new form $My_Form = New-Object system.Windows.Forms.Form # Define the size, title and background color $My_Form.ClientSize = '500,300' $My_Form.text = "Coaxial!!!" $My_Form.BackColor = "#0000ff" # Create a Label named Title $Title = New-Object system.Windows.Forms.Label $Title.text = "Out!" $Title.AutoSize = $true $Title.width = 55 $Title.height = 10 $Title.location = New-Object System.Drawing.Point(20,20) $Title.Font = 'Microsoft Sans Serif,13' # Create a Button named Add_Button $Add_Button = New-Object system.Windows.Forms.Button $Add_Button.BackColor = "#0F4C81" $Add_Button.text = "Add" $Add_Button.width = 90 $Add_Button.height = 30 $Add_Button.location = New-Object System.Drawing.Point(360,250) $Add_Button.Font = 'Microsoft Sans Serif,12' $Add_Button.ForeColor = "#000000" $Add_Button.Visible = $true # Create a Button named Cancel_Button $Cancel_Button = New-Object system.Windows.Forms.Button $Cancel_Button.BackColor = "#ffffff" $Cancel_Button.text = "Cancel" $Cancel_Button.width = 90 $Cancel_Button.height = 30 $Cancel_Button.location = New-Object System.Drawing.Point(160,150) $Cancel_Button.Font = 'Microsoft Sans Serif,10' $Cancel_Button.ForeColor = "#000000" $Cancel_Button.DialogResult = [System.Windows.Forms.DialogResult]::Cancel # Add GUI elements to My_Form $My_Form.CancelButton = $Cancel_Button $My_Form.Controls.Add($Cancel_Button) $My_Form.controls.AddRange(@($Title,$My_Label,$Add_Button,$Cancel_Button)) # Create function for Add_Button click function Add_Changes { $My_Form.BackColor = "#000000" $Title.BackColor = "#0000ff" $Title.text = "Go!" $My_Form.Text = 'Adding changes ...' } # Link the function with Add_button $Add_Button.Add_Click({ Add_Changes }) # Display the form [void]$My_Form.ShowDialog() |