An Easy InputBox

by Mar 27, 2009

All user input and output normally occurs inside the PowerShell console. Simply access the .NET framework directly if you'd like to get back the same dialogs VBScript scripters are using. Here is the old-fashioned InputBox:

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$name = [Microsoft.VisualBasic.Interaction]::InputBox("Enter your name", "Name", "$env:username")
"Your name is $name"

And here is your MsgBox:

[void][System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')

$result = [Microsoft.VisualBasic.Interaction]::MsgBox( `
"Do you agree?", 'YesNoCancel,Question', "Respond please")

switch ($result) {
'Yes' { "Ah good" }
'No' { "Sorry to hear that" }
'Cancel' { "Bye..." }
}