Building a PowerShell GUI (Part 8)

If you would like to read the other parts in this article series please go to:

In my previous article in this series, I showed you how to output a list of virtual machine names in response to a button click. Doing so proves that a form can display dynamic data, but we still aren’t doing anything that is all that useful. I still plan to show you how to build a GUI based application that provides useful information, but before I do, I want to show you a few techniques for adding a bit of visual flair to the interface.

Let’s start out by displaying the output in a text box. As you may recall from the previous article, the form’s output is typed directly onto the form’s background. You can see what that looks like in Figure A. Technically there is nothing wrong with doing that, but we can display the output in other ways.

Image
Figure A: The form’s output is written in the middle of the dialog box.

Displaying the list of virtual machines in a text box is surprisingly easy to do. In fact, you can do it by changing a single word. Take a look at the DisplayVMs function that causes the list of virtual machines to be displayed in response to a button click:

#Define Button Click Function

Function DisplayVMs

{

$VirtualMachines = Get-VM

Add-Type -AssemblyName System.Windows.Forms

#Create text

$Label = New-Object System.Windows.Forms.textbox

$Label.AutoSize = $True

Foreach ($vm in $VirtualMachines){

                $NL = “`n”

                $Label.Text = $Label.Text + $VM.Name + $NL

                }

 

$Label.Location = new-object System.Drawing.Size(200,110)

$Form.Controls.Add($Label)

}

The script contains a line that reads:

$Label = New-Object System.Windows.Forms.textbox

This line previously read:

$Label = New-Object System.Windows.Forms.label

By changing the word Label to Textbox, I was able to change the way that the output was displayed. You can see what this looks like in Figure B.

Image
Figure B: The output is now displayed in a text box.

As you look at the figure above, you will notice that although the output has been displayed in a text box, there are some formatting problems. Specifically, the text box is way too small, and all of the output has been jammed together on a single line. Fortunately, this is easy enough to fix.

To display the text on multiple lines, there are two lines of code that need to be added. These lines are:

$Label.Size = New-Object System.Drawing.Size(150,150)

$Label.MultiLine = $True

The first of these lines defines the size of the text box, while the second line of code allows the textbox to display multiple lines of output. You can see the result in Figure C. It still isn’t perfect, but we are getting closer.

Image
Figure C: The text box now displays multiple lines of text.

The problem that we are running into now is that some of the virtual machine names are still jammed together. Thankfully, this too is an easy fix. You might recall that when we were displaying the output as a label, we had to insert `n after each virtual machine name, in order to start a new line. This doesn’t quite work when we switch from a label to a text box. The fix is to use `r`n instead of `n. This code is used to tell PowerShell that we want a carriage return and a new line. Here is the actual modification to the script:

$NL = “`r`n”

You can see the resulting output in Figure D.

Image
Figure D: We have fixed the line item problem.

Now, I want to show you how to add a bit of visual flair to the form that you have created. One way of doing so is to add a splash of color. How about we start out by changing the form’s background color?

As you may recall, I used a variable named $Form to define the characteristics of the form. If we want to add a background color to the form, we can do so by adding a line of code that sets the $Form.BackColor. For example, I could make the form’s background light blue by adding this line of code:

$Form.BackColor = “lightblue”

You can see the results in Figure E.

Image
Figure E: The form’s background is now light blue.

OK, that’s great, but now the button kind of disappears into the background. So what can we do about changing the color of the button? Well, we can use exactly the same technique, except that this time the background color needs to be applied to the button instead of to the form as a whole. If for example, you wanted to make the button light gray, you could add this line of code:

$Button1.BackColor =”LightGray”

You can see the results in Figure F.

Image
Figure F: The button is now light gray.

Personally, I think that the form looks fine now, but let’s suppose that we wanted to change the color of the “click the button” message. In order to do so, you have to set the foreground color of the label object. If for example, you wanted to color the label text to be dark blue, you could use this line of code:

$Label2.ForeColor = “DarkBlue”

You can see what this looks like in Figure G.

Image
Figure G: I have changed the text color to dark blue.

I realize that I have made a lot of changes to the script, so here is the complete code as it currently exists (a few lines were rearranged):

#Load Assemblies

[System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”) | Out-Null

[System.Reflection.Assembly]::LoadWithPartialName(“System.Drawing”) | Out-Null

$net = New-Object -ComObject Wscript.Network

 

#Define Button Click Function

Function DisplayVMs

{

$VirtualMachines = Get-VM

Add-Type -AssemblyName System.Windows.Forms

#Create text

$Label = New-Object System.Windows.Forms.textbox

$Label.Location = new-object System.Drawing.Size(200,110)

$Label.Size = New-Object System.Drawing.Size(150,150)

$Label.MultiLine = $True

$NL = “`r`n”

Foreach ($vm in $VirtualMachines){

                $Label.Text = $Label.Text + $VM.Name + $NL

                }

 

 $Form.Controls.Add($Label)

}

 

#Draw form

$Form = New-Object System.Windows.Forms.Form

 $Form.width = 525

 $Form.height = 350

 $Form.BackColor = “lightblue”

 $Form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D

 $Form.Text = “Hyper-V Virtual Machines”

 $Form.maximumsize = New-Object System.Drawing.Size(525,350)

 $Form.startposition = “centerscreen”

 $Form.KeyPreview = $True

 $Form.Add_KeyDown({if ($_.KeyCode -eq “Enter”) {}})

 $Form.Add_KeyDown({if ($_.KeyCode -eq “Escape”)

 {$Form.Close()}})

 

# Create Label2

$Label2 = New-Object System.Windows.Forms.Label

$Label2.AutoSize = $True

$Label2.Location = new-object System.Drawing.Size(20,50)

$Label2.ForeColor = “DarkBlue”

$Label2.Text = “Click the button to see a list of the virtual machines hosted on the server.”

 

 

#Create button

 $Button1 = new-object System.Windows.Forms.Button

 $Button1.Location = new-object System.Drawing.Size(30,70)

 $Button1.Size = new-object System.Drawing.Size(80,20)

 $Button1.BackColor =”LightGray”

 $Button1.Text = “OK”

 $Button1.Add_Click({DisplayVMs})

 

 

#Create the Form

$Form.Controls.Add($Button1)

$Form.Controls.Add($Label2)

$Form.Add_Shown({$Form.Activate()})

$Form.ShowDialog()

Conclusion

As you can see, PowerShell gives you the ability to display form output in a more visually appealing manner. In the next article, I want to continue the discussion by showing you how list boxes work.

2 thoughts on “Building a PowerShell GUI (Part 8)”

  1. I trying doing this thing using text file
    I have have content in text file Like
    Jignesh
    Prajapati
    Sandep
    Man

    but while showing in textbox its shows like this
    jignesh prajapati sandep Man

    it shows in one line
    how to display same as shown in text file

  2. This can be done, but I don’t know of an easy way of doing it. You have to insert a new line code into the text string in order to get the text to be displayed on a new line. If the text is coming from a text file, then this means writing code to count characters, look for spaces, and insert new line codes in appropriate places. Maybe I can write the code for that in a future article.

Leave a Comment

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top