Manage Learn to apply best practices and optimize your operations.

Keep the graphical input prompt or avoid a GUI display: How to decide

If you don't need a GUI display, PowerShell can be hosted in several hosting applications so you can use cmdlets instead of a graphical input prompt.

How can I show a graphical input prompt in PowerShell?

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 administration tools

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

Start the conversation

Send me notifications when other members comment.

Please create a username to comment.