home > notes > powershell > ShowUI [ printer-friendly version ]
Basics
Profile
Simple GUI
ShowUI

ShowUI

GOAL: Install ShowUI from the official ShowUI site and execute a sample script.

ShowUI is an open-source software suite that greatly simplifies the process of coding GUI elements in PowerShell. I have no personal involvement with ShowUI, but it looks very promising.

NOTE: Read this page before you start working through the steps of installing ShowUI, and decide whether or not this is something you want to pursue. I think it's neat, but it is a third-party software suite and PowerShell purists might not want to mess with it.

Download ShowUI

The ShowUI web site should be your complete reference for all things pertaining to ShowUI. Download the software as a zip file and save it to a folder on your machine. I downloaded ShowUI.1.4.zip.

Unblock the zip file

Windows knows the difference between files you create on your computer (documents, spreadsheets, etc.) and files you download from the Internet. Normally, this doesn't make a big difference; you download photos and look at them, download documents and read them, and so forth. When you download software that you're going to execute in the PowerShell environment, though, this distinction between locally-created scripts and downloaded scripts is important.

Find the ShowUI zip file on your machine, right-click it, and click 'Properties'. On the 'General' tab, at the bottom, you'll see a section that says "Security: This file came from another computer and might be blocked to help protect this computer." Next to that is a button labeled "Unblock". Click "Unblock", then "Apply", then "OK".

If you do not unblock the zip file, ShowUI WILL NOT WORK. You will be able to unzip the ShowUI software into the proper directory, but PowerShell will go nuts with security exceptions when you try to run it.

This seems odd, I admit, but I see that other PowerShell software suites work the same way. You download a zip file, unblock it, and unzip it into a directory to use it. BE ADVISED: By doing this, you are trusting the software you download not to be malicious. That decision is yours and you are responsible for any consequences that may ensue.

Oddly enough, the need to unblock the ShowUI zip file is not documented on the ShowUI site. Thanks a lot, guys.

Extract the ShowUI files

Extract the ShowUI files from the zip file into a PowerShell directory. If your name is fred, put the unpacked files here:

C:\Users\fred\My Documents\WindowsPowerShell\Modules
You may see a WindowsPowerShell folder in your My Documents folder, but you'll need to create the Modules folder inside the WindowsPowerShell folder.

Initial ShowUI Setup

Use this command in PowerShell to initialize ShowUI:

Import-Module ShowUI
    
This should kick off a first-time initialization of ShowUI. You should see various setup messages and progress bars, and then you should be dropped back to the command line in PowerShell.

IF THAT DOESN'T HAPPEN, SOMETHING WENT WRONG. Did you unblock the zip file? You can blow away the Modules folder and try again.

Import ShowUI in your profile

If you're going to play with ShowUI, you want to make it available to you every time you start PowerShell. Add this line to your PowerShell profile file to import the ShowUI module when you start PowerShell:

Import-Module ShowUI
    
Edit your profile (you created one, right?) in Notepad:
notepad $profile
    

Set your PowerShell environment to STA

You're welcome to try and run the simple example code provided by ShowUI, but it won't work. You'll get an error that says "The calling thread must be STA, because many UI components require this."

What is STA, you ask? "Single-threaded apartment," apparently. Don't ask me where they came up with "apartment". You just need to invoke PowerShell with a -sta argument to make it run in single-threaded mode.

Locate your PowerShell icon (desktop shortcut), right-click it, and click on 'Properties'. On the 'Shortcut' tab, click in the 'Target:' box and add a space and -sta after powershell.exe at the end of the full path to the PowerShell executable file. Click 'Apply', then OK. REMEMBER THAT YOU HAVE DONE THIS. To go back to multithreaded mode, just repeat these steps and remove the trailing space and the -sta argument from the target path for the PowerShell executable.

Run the sample script

Here's the example script - a GUI wrapper that lets you specify a date and a log type to feed to the Get-Eventlog command (download):

$getEventInput = StackPanel -ControlName 'Get-EventLogsSinceDate' {            
    New-Label -VisualStyle 'MediumText' "Log Name"            
    New-ComboBox -IsEditable:$false -SelectedIndex 0 `
       -Name LogName @("Application", "Security", "System", "Setup")            
    New-Label -VisualStyle 'MediumText' "Get Event Logs Since..."            
    Select-Date -Name After            
    New-Button "Get Events" -On_Click {            
        Get-ParentControl |            
            Set-UIValue -passThru |             
            Close-Control            
    }            
} -show            

Get-EventLog @getEventInput

NOTE: Look closely at the New-ComboBox line in the code above. See how it ends with a backtick (`) character? That's not really part of the New-ComboBox command - it's the PowerShell line continuation character. The New-ComboBox command is a one-line command that continues clear through the list of options in quotes on the following line, but I broke it into two lines to make it look better on the Web page - and you've learned a new trick in the process.

This little GUI selector lets you specify a date and a log type and actually retrieves a list of events from the specified log. Note that the script coding is vastly simplified - ShowUI is doing all the heavy lifting for you.

Summary

Creating GUI elements in PowerShell is a lot easier with ShowUI. I'm told that you can develop entire GUI apps with ShowUI, and it's definitely something that warrants further study. The ShowUI web site contains documentation and instructional videos.


home Always know how to get out of Dodge. privacy