Tome's Land of IT

IT Notes from the Powertoe – Tome Tanasovski

Category Archives: ShowUI

New-SimpleForm – Holy dynamic GUI Batman!

Just like everyone else in the Windows PowerShell community I found what James Brundage and Joel “Jaykul” Bennett were doing with scripting UIs extremely interesting.  However, I found the whole process cumbersome for some reason.  Perhaps it was due to “meh” documentation.  Perhaps it was due to my brain just not being ready to absorb it.  Perhaps it was due to the fact that I can develop a winform really quickly in something like Primal Forms.  Whatever it was, it just didn’t seem like it was worth my effort.  Even after I watched James give a presentation on scripted GUIs at this year’s Techstravaganza in NYC I saw the potential, but I did not feel compelled to waste any more time with WPK than the few days I gave it when the PowerShell Pack was released.  Recently, the two major players in the scripted UI space joined forces with Doug Finke and developed a new set of cmdlets that took the great elements of each implementation of interfacing with WPF and combined them into one extremely sleek and impressive module.  In my mind this must have played out like this:  Doug Finke, successful enterprise-level developer who focuses on automation in the development space grabbed two mad scientists by the back of their necks and forced them down from the stratosphere where their genius lives to focus them to work in the world with the rest of us.  Perhaps there were chains involved?  Perhaps shock treatment?  Perhaps it didn’t even happen like that and was more like James’s blue ox cut down Jaykul’s apple tree and little red riding Doug couldn’t tell a lie.  We’ll let the myth of the why perpetuate.  What’s important is that I am impressed with their collaborative effort, and I can’t wait to share what I did in only 60 minutes with the new stuff.

If you are not aware of what I’m talking about and these names just sound like nonesense to you welcome to the niche world of scripted UIs.

The Promise of Scripted UIs

The promise is simple.  Dynamically generated user interfaces that adapt to your needs.  I had the rare opportunity to see some of James Brundage’s WPK-based cmdlets that would convert seemingly simple bits of code into beautiful graphics and doo dahs and woo hoos.  He was developing a library of these to help him very rapidly develop based off of these prototypes.  It appears that now that this is within all of our reach.

The ShowUI toolkit

I won’t be going into the introduction of how to use this module.  I’ll leave that to you.  There’s a few places to start though.

  • Download ShowUI from the codeplex site
  • Read the amazing tutorial that Joel “Jaykul” Bennet put up on his site (to be honest – this is as far as I’ve gotten so far).  Actually his other posts on the subject look good too.
  • Watch the videos that James Brundage put together on his site
  • Read the articles that Doug Finke has been posting.  The twitter client looks bad-ass!  He also has a video up.


The one thing you’ll notice when going through the sample code is that everything is written in scriptblocks.  This is the power of the toolkit.  It is based on Ruby Shoes – a toolkit that was created to make UIs for Ruby.  The syntax looks nearly identical.  But that’s a digression.  More importantly is that scriptblocks can easily be dynamic because they are just code.  A while back I posted this article that talks about the world of dynamic scriptblocks in Windows PowerShell.  I took that and realized that I could easily apply it to the sample in Joel’s tutorial that stood out the most to me:  The simple form that displays label/textbox, label/textbox, label/textbox, etc, followed by a button to submit.  The magic was that not only was this being generated, but after hitting submit it returned a hash table that had the values that were entered in the text boxes.  So, I said to myself, “Well I want this dynamic.  I want to pass a random number of fields and have it generate this simple form.  The result is the function below New-SimpleForm.

Import-Module showui            

function New-SimpleForm {
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [string]$Name = 'Simple ShowUI Form',
        [int] $MinWidth = 250
    BEGIN {
        $allfields = @()
        $allfields += $Fields
    END {
        $txtblock = ""
        $row = 0
        foreach ($field in $allfields) {
            $boxlabel = $field -replace '\W' ,'_'
            $txtblock += "Label '$field' -row $row `r`n"
            $txtblock += "TextBox -Name '$boxlabel' -Column 1 -Row $row`r`n"
        $txtblock += @" Button "Submit" -IsDefault -On_Click { Get-ParentControl | Set-UIValue -passThru | Close-Control } -Row $row -column 1 "@
        $scriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock($txtblock)
        $onloaded = $ExecutionContext.InvokeCommand.NewScriptBlock("`$$($allfields[0] -replace '\W', '_').Focus()")
        grid -ControlName $Name -Columns Auto,* -Rows ($allfields.count + 1) -MinWidth $MinWidth $scriptblock -show -On_Loaded $onloaded


New-SimpleForm can be called like this:

@("Name", "E-mail", "Date of Birth") | New-SimpleForm

The above will produce this form:

The output will be a hash table that looks like this:

Name                           Value
----                           -----
Name                           Tome Tanasovski
Date_of_Birth                  5/11/1976

Alternatively you could supply the hash table right into a psobject if you prefer:

new-object psobject -Property (@("Name","E-mail","Date of Birth") |New-SimpleForm)

The output of the above looks like this:

Name                         Date_of_Birth                E_mail
----                         -------------                ------
Tome Tanasovski              5/11/1976          

Not bad for only 60 minutes of tinkering. Now I have a utility webform that I can use to retrieve user input extremely quickly. Obviously, validation has to come after the fact, but that’s not so bad. Whew! Feels good to be playing with interesting things!

———————-Update 7/6/2011, 8:55 PM————————–

ShowUI version 1.1 was just released with a cmdlet called Get-Input.  This takes my idea and blows it out of the water!  Check out the video of it here.

%d bloggers like this: