Hey, Scripting Guy! Using the Windows Presentation Foundation PowerShell Kit to Create a GUI


Bookmark and Share 

About the author: Doug Finke is a Microsoft PowerShell MVP working for Lab49, a company that builds advanced applications for the financial service industry. Over the last 20 years, Doug has been a developer and author working with numerous technologies. You can catch up with Doug on his blog.

 

The Scripting Guys recently posted Weekend Scripter: Creating a GUI for a Windows PowerShell Script, in which Ed Wilson builds a Windows form GUI application for a Windows PowerShell script. In today’s post, I’ll build the same GUI application but with the Windows Presentation Foundation PowerShell Kit (WPK).

About WPK

WPK allows you to build rich user interfaces entirely with Window PowerShell scripts. WPK provides more than 600 cmdlets, which allow you to add WPF buttons, grids, list boxes, and more to Windows forms.

James Brundage, author of WPK, says “WPK pays homage to Tcl/Tk.” Tcl (pronounced “Tickle”) is a programming language created in 1990 by John Ousterhout when he was a professor at the University of California, Berkeley.

Hello World in WPK

You need to download and install the Microsoft PowerShell Power Pack to try out these examples.

WPK provides access to the presentation layer in WPF, surfacing controls in the standard Verb-Noun form. This example shows how you can add a label to a window and display, “Hello World.” In addition, each control has parameters. For the New-Window cmdlet, we are using four parameters: Height, Width, Show, and Content. In the example, the Content parameter is implied so that we can shorten the script by leaving it out. It would come after the Show parameter.


Import-Module WPK

New-Window -Height 125 -Width 300 -Show {

    New-Label "Hello World" -FontSize 50

}

 


Image of Hello World window

 

The Trip Cost WPK GUI

Image of Trip Cost WPK GUI


The Code

First, you need to download WPK from the MSDN Code Gallery. WPK lets you create rich user interfaces quick and easily from Windows PowerShell. Think HTA, but easy.


Import-Module WPK

New-Window -Title "Trip Cost" -WindowStartupLocation CenterScreen `

  -Width 300 -Height 200 -Show {

 

  New-Grid -Rows 32, 32*, 32*, 32* -Columns 100, 100* {

    New-Button -Content "_Calculate" -Row 0 -Column 0 -Margin 3 -On_Click {

      $txtTotalCost      = $Window | Get-ChildControl txtTotalCost

      $txtMiles          = $Window | Get-ChildControl txtMiles

      $txtMilesPerGallon = $Window | Get-ChildControl txtMilesPerGallon

      $txtCostPerGallon  = $Window | Get-ChildControl txtCostPerGallon

     

      $txtTotalCost.Text = `

        "{0:n2}" -f (($txtMiles.Text / $txtMilesPerGallon.Text) `

         * $txtCostPerGallon.Text)

    }

   

  New-TextBox -Name txtTotalCost `

    -Row 0 -Column 1 -Margin 3

   

  New-TextBlock -Text "Miles" `

    -Row 1 -Column 0 -VerticalAlignment Center -Margin 3       

       

  New-TextBox -Name txtMiles `

    -Row 1 -Column 1 -Margin 3 -Text 100

 

  New-TextBlock -Text "Miles Per Gallon" `

    -Row 2 -Column 0 -VerticalAlignment Center -Margin 3

 

  New-TextBox -Name txtMilesPerGallon `

    -Row 2 -Column 1 -Margin 3 -Text 23

 

  New-TextBlock -Text "Cost Per Gallon" `

    -Row 3 -Column 0 -VerticalAlignment Center -Margin 3

  New-TextBox -Name txtCostPerGallon `

    -Row 3 -Column 1 -Margin 3 -Text 2.50

  }

}


The Code Explained

WPK is a Windows PowerShell module. To use a module, you first need to import it using the Import-Module cmdlet.

 

The Trip Cost GUI uses these WPK cmdlets:

 

New-Window

Creates a new System.Windows.Window

New-Grid

Creates a new System.Windows.Controls.Grid

New-Button

Creates a new System.Windows.Controls.Button

New-TextBox

Creates a new System.Windows.Controls.TextBox

New-TextBlock

Comments (8)

  1. Anonymous says:

    Hi again,

    I got it. I just had to cast the variables. e.g. [int]

    Cheers

  2. I know it's a few years too late, but thanks for sharing, someone just referred me here from social.technet.

  3. Anonymous says:

    Hi,

    Sorry about the question. I’m just starting with the PowerShell.

    I’m trying to make a modified version of the The Trip Cost WPK GUI.

    The problem is when I try to sum the values with the operator ‘+’, it gives the result as a string. I know that it should give the result as a string as it calls the .Text property but in your example it does the math.

    So if I put <code> $TotalCost.Text = $fuelcost + $food + $accomodation <code>

    and then I enter 50 in the fuelcost textbox, 50 in the food textbox and 50 in the accomodation textbox, when a click Calculate it should give me 150, but it gives 505050

    I tryed to remove the Text property but it throws an error saying that there’s no addition operation set in the New-TextBox function.

    Any ideas?

    Thanks

  4. Kevin A. says:

    I know this is years too late. What happened to the WPK module, i have a need for it but cannot find it anywhere.

    K

  5. David says:

    Article would be interesting if you used pure powershell. Building tools that depend on a module to which links on internet went down in a system that lacks package manager => 🙁
    As soon as I’ve seen "Import-Module blablabla" it stopped to be worth reading.
    I can write write a tutorial how to send rocket to Mars in powershell:

    Import-Module UniverseRocketController
    Invoke-SendRocket -Destination Mars -RocketType MilkyWayTransporter -Crew (Get-RocketCrew)

    See? Two lines of code. You just need to download and install one module that is not part of powershell yet.

  6. Kobayashi Maru says:

    Such a piss taker ‘david’ but funny 🙂

  7. Dan Jackson says:

    Is there anywhere to get the WPK now? The Microsoft link seems to be dead.

  8. diggy says:

    It’s been replaced by the showUI module. This script works fine with showUI.
    https://showui.codeplex.com/

Skip to main content