How can I show a graphical input prompt in PowerShell?
By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
First, ask yourself if that's what you really want to do.
PowerShell itself is designed to be hosted inside of many different hosting applications. The PowerShell.exe interface you may be used to; the Console host is one such host. The PowerShell ISE is another. But there are more, including the Wsmprovhost.exe that's used to host PowerShell when you use remoting to connect to a remote computer.
Different hosts have different capabilities, and some of them run in different contexts. For example, Wsmprovhost.exe isn't something you'll ever see or interact with directly; it runs on a remote computer, receives your commands, executes those commands and transmits results back to you. Popping up a graphical dialog box within that host won't work, because there's nobody to see it.
PowerShell scripts remain portable when you don't use GUI displays. Instead, use Read-Host. Some hosts will implement that as a command-line input prompt; others may implement it as a graphical dialog. The host application decides, based on its context and usage environment.
If you need to pop up a graphical input box, you can. You just have to access the .NET Framework:
[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
$computer = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a computer name", "Computer", "$env:computername")
That example even shows you how to prefill a default value for the input prompt. Give it a try. It doesn't do anything dangerous, and it'll let you see how the InputBox function works.
Understand that the graphical input box isn't a PowerShell thing -- it's "borrowing" from .NET's VisualBasic code.
About the author
Don Jones is a well-known and respected PowerShell expert and educator. He's co-author of three books on PowerShell (see PowerShellBooks.com for a list). You can find his content online, including his PowerShell Q&A forums, by visiting DonJones.com.
Dig Deeper on Windows PowerShell Scripting
Related Q&A from Don Jones
With some simple configuration, admins can run a PowerShell script each time a computer starts with the help of the Task Scheduler GUI and cmdlets.continue reading
Each PowerShell version has different features made useful by add-ins and snap-ins, so admins have to pay attention to what features work where.continue reading
Wondering how to get PowerShell to read XML files? Our expert explains a few methods you could use.continue reading
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.