3 min read
Designing PowerShell GUIs with Windows Forms for Interactive Scripts
PowerShell has full access to the .NET Framework and can therefore utilize all the graphical functions available in...
Unlocking the Power of PowerShell: Tips for Success
Scripts that run unattended in the background do not need user interfaces. However, scripts can also serve as tools for tasks like conversions, report generation—you name it. Adding a user interface to such scripts can be an inviting way to let users of all levels benefit from their capabilities.
Here are some of your options:
In this article series, we’ll explore each variant and provide code samples to help you quickly evaluate each option.
When user interfaces are composed using simple text, they work in any PowerShell console on any operating system, giving you maximum design freedom—but also all the work.
For simple choice prompts, you can take advantage of PowerShell’s built-in API by using the PromptForChoice() method, which handles most of the heavy lifting.
However, making this method truly user-friendly and intuitive takes some effort. Here's a wrapper function called Get-Choice that helps you generate flexible choice prompts:
function Get-Choice
{
[CmdletBinding(DefaultParameterSetName="YesNo")]
param
(
[Parameter(Mandatory)]
[string]
$Prompt,
[Parameter(Mandatory,ParameterSetName="Choices")]
[string[]]
$UserChoices,
[Parameter(ParameterSetName="YesNo")]
[switch]
$YesNo,
[Parameter(ParameterSetName="YesNoCancel")]
[switch]
$YesNoCancel,
[string]
$Title='Windows PowerShell',
[int]
$Default=0
)
switch ($PSCmdlet.ParameterSetName)
{
"YesNo" { $UserChoices = "&Yes", "&No" }
"YesNoCancel" { $UserChoices = "&Yes", "&No", "&Cancel" }
Default { }
}
$result = $Host.UI.PromptForChoice($Title, $Prompt, $UserChoices, $Default)
return [PSCustomObject]@{
UserChoice = $result
SelectedItem = $UserChoices[$result].Replace('&','')
}
}
Creating text-based choice prompts can be as simple as this:
PS C:\> Get-Choice -Prompt 'Do you want to restart the computer now?'
Windows PowerShell
Do you want to restart the server?
[Y] Yes [N] No [?] Help (default is "Y"): n
UserChoice SelectedItem
---------- ------------
1 No
Thanks to the parameters exposed by Get-Choice, you can easily display more options, add a title, and change the default selection.
PS C:\> Get-Choice -Prompt 'Do you want to restart the server?' -YesNoCancel -Title 'Important' -Default 2
Important
Do you want to restart the server?
[Y] Yes [N] No [C] Cancel [?] Help (default is "C"):
UserChoice SelectedItem
---------- ------------
2 Cancel
You can even create custom choice prompts of your own:
PS C:\> Get-Choice -Prompt 'Which city to connect to?' -UserChoices '&New York', 'L&ondon', 'Hanno&ver' -Title 'VPN' -Default 1
VPN
Which city to connect to?
[N] New York [O] London [V] Hannover [?] Help (default is "O"): v
UserChoice SelectedItem
---------- ------------
2 Hannover
The ampersand (“&”) sets the keyboard shortcut for your custom choice item.
PromptForChoice() is an excellent, flexible, and easy-to-use option for creating simple text-based choices that work across all PowerShell versions and operating systems—especially when wrapped in a function like Get-Choice.
Note that PromptForChoice() may behave differently in non-console hosts; for example, in PowerShell ISE, it generates graphical dialogs.
PromptForChoice() is just a simple appetizer. If you're intrigued, you can create much more sophisticated menu structures. Simply search for “PowerShell console menu function,” and you'll find plenty of ready-to-use examples, functions, and even modules that generate all kinds of text-based menus.
For even more complex text-based menus—reminiscent of the good old Norton Commander—you can use TUIs (text-based user interfaces). Since TUIs are programmed similarly to graphical user interfaces (GUIs), we'll look at GUIs first in the next part.
Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.
The poster is available for download and in paper form.
May 14, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
PowerShell has full access to the .NET Framework and can therefore utilize all the graphical functions available in...
May 9, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
Scripts that run unattended in the background do not need user interfaces. However, scripts can also serve as tools for...
Apr 30, 2025 by Aleksandar Nikolić and Dr. Tobias Weltner
In the previous part, we attempted to create a tool that lists all .NET methods used in a script. While this worked...
Tobias Weltner and Aleksandar Nikolić joinly wrote the blog post series 'Tobias&Aleksandar's PowerShell tips'. So we introduce both of them here:
----------------------------
Aleksandar Nikolić is a Microsoft Azure MVP and co-founder of PowerShellMagazine.com, the ultimate online source for PowerShell enthusiasts. With over 18 years of experience in system administration, he is a respected trainer and speaker who travels the globe to share his knowledge and skills on Azure, Entra, and PowerShell. He has spoken at IT events such as Microsoft Ignite, ESPC, NIC, CloudBrew, NTK, and PowerShell Conference Europe.
----------------------------
Tobias is a long-time Microsoft MVP and has been involved with the development of PowerShell since its early days. He invented the PowerShell IDE "ISESteroids", has written numerous books on PowerShell for Microsoft Press and O'Reilly, founded the PowerShell Conference EU (psconf.eu), and is currently contributing to the advancement of PowerShell as member in the "Microsoft Cmdlet Working Group". Tobias shares his expertise as a consultant in projects and as a trainer in in-house trainings for numerous companies and agencies across Europe.