PowerShell GUIs: Where do I start?
- Written by June Blender
- Last Updated: 11 July 2016
- Created: 16 July 2015
- Hits: 2891
I’ve been using Windows PowerShell since the very first version of Monad in 2005, but PowerShell-based applications with a graphic user interface (GUI) are a whole new world. Instead of thinking of cmdlets and parameters or properties and methods, you need to think in object events. And because there’s no console with .ps1xml-directed views, you need manage all output streams, including standard output and errors.
Most importantly, because you’re dealing with real users, you need to assume that people will type and click whatever they please in whatever order you let them. It reminds me of turning the car over to my teenagers for the first time. In a GUI app, the user drives. So, if you don’t want them to go somewhere, you need to enable and reveal parts of the interface selectively.
PowerShell Studio automates the complex parts, including loading assemblies, instantiating objects, and registering event handlers, so you can focus on making great PowerShell GUI apps.
Here are some of the fabulous resources that I used to help me learn how to create PowerShell GUIs. If you have a favorite book, tutorial, or blog post, comment and I’ll add it to this post.
My First PowerShell GUI
If you’re starting from scratch, start here:
- My First Form: Build a Simple PowerShell GUI app with PowerShell Studio by SAPIEN Tech (video)
Follow along as we build a simple GUI app that displays the versions of Windows and Windows PowerShell on the local computer.
- My First Form: Controls and Properties by SAPIEN Tech (video)
Learn about the Windows Forms controls that you use in a GUI application and how to set their property values.
- Building GUI for PowerShell Scripts using PowerShell Studio 2012 by Vinith Menon
Step-by-step instructions for creating a GUI app that runs Get-WMIObject commands
- PowerShell Studio 2012 – WinForms – Creating a basic GUI by Francois-Xavier Cat
A multi-part video tutorial on making a GUI for any script
- Learn Windows PowerShell Toolmaking in a Month of Lunches by Don Jones and Jeffery Hicks
In Chapters 23 – 25, Don and Jeff build a GUI tool from scratch, first by using Windows Forms classes and then in PowerShell Studio.
- Windows Forms PowerShell Gui Template by Nickolaj Andersen
Nickolaj provides a template for creating a form object in Windows PowerShell. PowerShell Studio does all of this work for you, but it’s useful to see and understand what goes on under the covers.
Basic GUI App Techniques
Now that you have the basics, avoid the most common errors encountered by scripters moving to GUI apps:
- Displaying Output in a GUI Application
There’s no console or automatic display in a GUI app. If you don’t assign output to a UI element, the user never sees it.
- Managing Errors in a GUI Application
Once you’ve mastered standard output, learn your options for managing errors.
- Creating a GUI for CSV Data
Learn how to display data that originates in CSV format. Import-Csv and then… ?
- Create a PowerShell GUI with a Fixed-Size UI (video)
The simplest GUIs have a form that the end-user can’t resize. Keep it simple!
Easy, but Sophisticated
These easy and quick techniques make your GUI apps more usable. They take just minutes to master.
- Adding Auto-Complete to an Input Textbox
Auto-complete you textbox input dynamically based on the user’s file system, available web URLs, or custom data, like servers, services, and processes.
- Copy to Clipboard in a GUI Application
Use a one-liner to add a “Copy to Clipboard” button to any app. It’s a great companion to a output textbox.
- Typing Enter Pushes a Button
Let the user press Enter instead of having to click a button. Create a KeyDown event for a textbox or use the AcceptButton property of the form.
Best Practices for UI Design
Where do I put that button? Should I center the textbox? You might be new to GUI app development, but your GUI apps don’t have to advertise it. Learn from user experience and human factors engineers about best practices for UI design.
- About Face: The Essentials of Interaction Design 4th Edition by Alan Cooper, Robert Reimann, David Cronin, & Christopher Noessel
Learn the principles of UI design from people who study user-computer interaction. An excellent book for beginners and experienced designers.
- 9 Things I Learned in UX Grad School by Sara Ford
Sara summarizes the practical elements of academic UX research for the UI designer, including “Why ‘Developer Speak’ in UI is so bad ” and “Why people make errors”
- User Interface Design for Administrators
When do you use a button? When are radio buttons better than checkboxes? SAPIEN Chief Technology Officer, Alexander Riedel, creates a control table that you’ll refer to again and again.
- Single Form or Multiple Dialogs?
How much is too much on a single form? Learn the guidelines that professional UI developers use to make this critical decision.
- Design Better Forms by Andrew Coyle
Great do's and don't's from a professional UI designer.
- How I learned to love DialogResult
The DialogResult property closes my form abruptly. Why shouldn’t I just set its value to “None?” Learn about the best-practice standards that DialogResult supports.
Those Nagging Questions
- PowerShell GUI: Why think in events?
A PowerShell GUI application consists of an unordered collection of event handlers. It’s so different from traditional scripting that you learn to think in a whole new way.
- Scope in a PowerShell GUI App
Why does each event handler run in its own scope? Learn how to share variables between event handlers.
- The Methods That Register Events
What’s the source of the methods that register events, like $buttonClose.add_Click()? MSDN doesn’t list them and Get-Member doesn’t return them.
Troubleshooting & Debugging
- PowerShell GUI Debugging Tip: Duplicate Event Handlers
If your event handler doesn’t work, verify the control names. If they’re correct, you might have a duplicate event handler.
- How I Learned to Love DialogResult
If your form closes immediately when you click OK (or another standard button), instead of staying open so that you can use it, it might be caused by the value of the DialogResult property of the button.
One-A-Day: Explore the Windows Forms Controls
One of the best, and easiest, ways to learn the UI controls, is to read our Spotlight articles and try the examples. You can read the MSDN documentation about Windows Forms controls, but our Spotlight articles, designed for PowerShell scripters, focus on the most important features of each UI control, and get you going with PowerShell examples.
Use your “Month of Lunches” habit to explore one new control each day.
In PowerShell Studio, to open a Spotlight article about a control:
- In the Toolbox or Property pane, right-click a control and then click View Spotlight Article.
- Or, in the Designer window, right a control, click Help, and then click View Spotlight Article.
Form ImageList StatusBar Button ListView StatusStrip Textbox MaskedTextBox TabControl ComboBox NotifyIcon Timer CheckBox PictureBox ToolTip ContextMenuStrip ProgressBar TreeView ErrorProvider RadioButton
If you have suggestions that might help other scripters, leave a comment or tweet it to me at @juneb_get_help.