Powershell

How to Create GUIs with PowerShell?

PowerShell is made up of scripting languages, and configurations for framework management. It provides a cross-platform for the automation of tasks. However, PowerShell GUIs are still not interactive with users as in other languages. It lags in the user interface. Only those people who have expertise in PowerShell are using it.

This article will provide information about creating GUIs on PowerShell.

How to Create GUIs With PowerShell?

GUI is a graphical user interface. It is the interaction of graphical components and conveys visual information to users about the actions they can take and the options they have. Script results of PowerShell commands are shown in the PowerShell console terminal, not in the command window which is difficult to understand. GUIs make it easy to understand by involving graphical elements of PowerShell using frameworks of Windows. Commonly .NET Windows forms(WinForms) are used for the graphical user interface in PowerShell. There are many other types of libraries available for creating GUIs, the most common is Windows Presentation Foundation(WPF).

Example: GUI Creation Using PowerShell

Window Forms(WinForms) are used for the creation of GUI using PowerShell. This example visualizes a calendar of default size in the custom-created dialog box. The Script selects the present date by default. Users can select any date as in normal calendars and after pressing the run button selected date in the format of day, month, and year is printed on the custom-created dialog box. This example will display the creation of a GUI in PowerShell.

Initially, press the Windows key search PowerShell, and click on Run ISE as Administrator:

Now, create a new script file by clicking the new file icon in the top left of the opened window:

Afterward, create a Window using PowerShell. To do so, load the System.Windows.Forms to use WinForms objects and create new custom windows by using $objForm = New-Object Windows.Forms.Form:

$objForm = New-Object Windows.Forms.Form

$objForm.Text = "Date Selection"
$objForm.Size = New-Object Drawing.Size @(300,300)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True
[void] $objForm.ShowDialog()

A window of (300,300) size as width, and height has been generated and we assigned the title Date Selection to it.

Next, specifying functions for generating data on custom custom-created dialog box. Here is the complete code for the display of GUI on PowerShell:

$objForm = New-Object Windows.Forms.Form

$objForm.Text = "Date Selection"
$objForm.Size = New-Object Drawing.Size @(300,300)
$objForm.StartPosition = "CenterScreen"

$objForm.KeyPreview = $True

function btnselect
{
            $dtmDate=$objCalendar.SelectionStart.ToString("dd MM yy")

            Write-Host "Date selected: $dtmDate"


        $ps = new-object System.Diagnostics.Process
        $ps.StartInfo.Filename = "cmd.exe"
        $ps.StartInfo.Arguments = " /c echo Date = $dtmDate"
        $ps.StartInfo.RedirectStandardOutput = $True
        $ps.StartInfo.UseShellExecute = $false
        $ps.start()
        $ps.WaitForExit()
        [string] $Out = $ps.StandardOutput.ReadToEnd();

        Write-Host "Output $Out"
        $label.text = "$Out"
}
[void] $objForm.ShowDialog()
$button = New-Object Windows.Forms.Button
$button.text = "RUN"
$button.Location = New-Object Drawing.Point 100,180
$button.add_click({btnselect})
$objForm.controls.add($button)

$label = new-object system.windows.forms.label
$label.text = ""
$label.Location = New-Object Drawing.Point 100, 210
$label.Size = New-Object Drawing.Point 200,30
$objForm.controls.add($label)

$objCalendar = New-Object System.Windows.Forms.MonthCalendar
$objCalendar.ShowTodayCircle = $False
$objCalendar.MaxSelectionCount = 1
$objForm.Controls.Add($objCalendar)

$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

In the above code initially, we generated a blank Dialog box with the title Date Selection and specified its size as (300,300) which is its width and height. We also created a new function btnselect. The calendar is specified in the created dialog box, and we can specify a date of our own choice. The provided date will be shown by clicking on the “Run” button. As a result, the date will be printed in the dialog box and terminal:

The output image is shown in the above image. we can adjust the placement, size, and components of icons in the custom-created dialog box.

Conclusion

PowerShell is advanced and it has integration with .NET objects. PowerShell uses WinForms applications for creating a GUI. WinForms has built-in .NET objects for the user interface. The use of GUI makes PowerShell more readable and easier for those users who have less development experience on PowerShell. There are other frameworks also available for creating GUI in PowerShell. This blog demonstrated an efficient method for creating a GUI on PowerShell.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.