Q

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.

This Content Component encountered an error

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.

This was first published in August 2013

Dig deeper on Windows PowerShell Scripting

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

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.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

-ADS BY GOOGLE

SearchServerVirtualization

SearchCloudComputing

SearchExchange

SearchSQLServer

SearchWinIT

SearchEnterpriseDesktop

SearchVirtualDesktop

Close