Converting VBScript's InputBox Function

Definition: Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns the contents of the text box.


OK, this was a tough one. At first we figured that the easiest way to create an input box would be to rely on the .NET Framework and Visual Basic .NET. That worked, but with one major problem: the input box always appeared behind all the other windows on the desktop. That meant that the input box would appear, but no one would ever see it. Not exactly what we were hoping for.

At our wit’s end, we did what anyone else would do in our situation: we started searching the Internet looking for sample code we could steal. And, sure enough, we found some, courtesy of /\/\o\/\/, a Windows PowerShell MVP. (Check out his Web site and blog; when it comes to Windows PowerShell, this guy knows what he’s talking about.) Here’s the solution /\/\o\/\/ came up with:

$a = new-object -comobject MSScriptControl.ScriptControl
$a.language = "vbscript"
$a.addcode("function getInput() getInput = inputbox(`"Message box prompt`",`"Message Box Title`") end function" )
$b = $a.eval("getInput")

We won’t bother trying to explain how this works; we’ll simply note that it does work. We’ll also point out that you have to type this exactly as shown; in particular, that means including all the little backticks (the ` character) when assigning a message box prompt and title.

By the way: thanks, /\/\o\/\/!

Return to the VBScript to Windows PowerShell home page