Creating your first Powershell GUI – Part 1

Already seen this part? Check out Part 1, Part 2, and Part3

Powershell, while a powerful tool can sometimes lack that person touch that a GUI interface offers. Console output is fine and dandy, but there are times when parsing through a large amount of data can be done with a GUI and some simple commands.

From here, I’m going to assume you know the basics. Such as awareness of the get-executionpolicy and set-executionpolicy commands, and a little knowledge of PowerShell ISE.

The Setup

I’m doing this with my home lab from a non-domain computer. Which means I’ll be using some credential switches you may not need.

For this example, we’ll take something simple, like

Get-ADUser

Get-ADUser gets a user object or searches to get multiple user objects from Active Directory. To keep this simple, we’ll have it search up a name. You’ll get the idea of what I’m doing and how we can keep it somewhat simple.

First, we’ll get a simple query working. Let’s search for a user named ‘Rick’

get-Aduser -Filter {name -like '*rick*' } -Server 10.0.0.101 -Credential "contoso.com\administrator"

Explanation of the above query:

-Filter {name -like '*rick*'}

Search for a username using wildcards in-front and behind of name we’re looking for.

-Server 10.0.0.101

Look at a specific Domain controller. Since this is on a different subnet for me, I’m specifying it by IP. You can simply use the hostname of the domain controller, or you can use “-SearchBase” and plug in the domain name itself.

-Credential "contoso.com\Administrator" 

Provide a specific set of credentials with read access to the domain. Since I’m issuing these commands from an non-domain machine, this is handy for me in the testing phase.

Put this or something very close to this into PowerShell ISE. Highlight the code and hit F8 to run the selection. This just ensures you have something to search for. Obviously, I chose ‘Rick’ because that name exists in my Domain, you’ll have to pick or create one to search.

So now we have the format of the query, how can we turn it into a GUI?

Visit POSHGUI.com. This is a free website that creates the Powershell forms for you. All you need to do is create the code to plunk in. Goto WinForms Designer.

Now on the form drag a button and a text box. For this example, we’ll want the text box a little bigger – which means in the ‘behavior’ section, we need multi-line. You should have something similar to the below:

ön

I kept the default values for each. They should be button1 and textbox1 respectively from here on.

What we want to happen: When pressing ‘Search Up “Rick”‘ button, the results should show in the text box below.

In PoshGUI click on the ‘Code’ button. This creates the entire form with coordinates, arguments and ranges.

1 • This form was created using POSÆU1.co• 
. NAME 
unti t led 
Add-Type -AssemblyNsme System Windous.Forns 
a free online gui designer for Power-shell 
[System. Windows . Forms . Application] : : EnableVisuaIStyIes() 
SForn. Clientsize 
SFcrn. text 
11 
SFcrn. Topmost 
12 
S Buttonl 
14 
ss uttcnl. text 
IS 
SS uttcnl.nidth 
IS 
SSuttcn1. height 
17 
SButtcn1. location 
IS 
SButtcn1. Font 
19 
STexteox1 
21 
SText20x1. multi line 
22 
STexteox1. text 
23 
STexteox1. nidth 
24 
SText20x1. height 
STexteox1. location 
STexteox1. Font 
27 
29 
Object 
Yeu Object 
- "Form" 
Sfalse 
Object 
"Search up 
Object 
Object 
Object 
St rue 
system .Windous . Forms . Form 
system. Windows . Forms . Button 
'"Rick' 
System. Drawing. Point(é2, 14) 
System.orauing. Font( •microsoft Sans Serif' , le) 
system. Windows . Forms . TextBox 
"Results show up here" 
Object System.orawing. 
Object System.orauing. Font( •microsoft Sans Serif' , le) 
ST extBox1

The above is a screenshot, I’ve added my code below with some additional explanation in between the comments.

$results = $null #reset the $results variable each run

Add-Type -AssemblyName System.Windows.Forms

[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(400,324)
$Form.text = “Form”
$Form.TopMost = $false

$Button1 = New-Object system.Windows.Forms.Button
$Button1.text = “Search up `”Rick`””
$Button1.width = 253
$Button1.height = 30
$Button1.location = New-Object System.Drawing.Point(62,14)
$Button1.Font = New-Object System.Drawing.Font(‘Microsoft Sans Serif’,10)
<#—–Calls a function ‘ADSearch’——-#>
$Button1.Add_MouseClick({ADSearch})

$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.multiline = $true
$TextBox1.text = “Results show up here”
$TextBox1.width = 338
$TextBox1.height = 261
$TextBox1.location = New-Object System.Drawing.Point(26,57)
$TextBox1.Font = New-Object System.Drawing.Font(‘Microsoft Sans Serif’,10)

$Form.controls.AddRange(@($Button1,$TextBox1))

[system.windows.forms.application]::run($form) #Runs form with runtimes – this is a trial and error thing

Function ADSearch {
<#——function ‘ADSearch’ runs Get-ADUser query, stores into $results variable——#>

$results= get-Aduser -Filter {name -like ‘*rick*’ } -Server 10.0.0.101 -Credential “contoso.com\administrator”

<#—–Display output to $Textbox1.text——-#>
$TextBox1.text = $results

}

Copy and paste that code, edit the query to whatever name exists in your Domain, and the IP to match whatever domain IP you have. Hit F5 to run the script, and you should see something similar.

Form 
Search up "Rick" 
Results show up here

Click the Mouse Button, enter your domain credentials and the results display:

Form 
Search up "Rick" 
CN=Rick

And that’s about it! It’s a simple script, and shows some of the very simple ways to display information in a GUI. This only needs PowerShell ISE, PoshGUI, and some curiosity.

If you want to make this a little fancier, take input from a user, and display specific information, join me for Part 2.

[ivory-search 404 "The search form 3350 does not exist"]

Leave a Reply

Your email address will not be published. Required fields are marked *

*

*

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.