Read-Host in PowerShell v3.0

by GuyThomas at 2013-01-18 09:50:51

I found an old machine with PowerShell v2.0, and Read-Host in the ISE version, causes an input dialog box to appear - good.
It amazed me that the same command in v3.0’ ISE did not display the input behaviour box - disappointing. Is this a change in behaviour? Is there a parameter to recover the old v 2.0 behaviour?
by DonJ at 2013-01-18 09:58:08
It’s a change, but not in the command.

It’s not a change in the PowerShell engine. The implementation of Read-Host and Write-Host are, as the command names imply, determined by the host applications. So it’s a change in the ISE - which is why there’s no parameter to change the behavior. The 3.0 ISE adopted a Console pane, and tries to more closely mimic the behavior of the Console host application.

Every application that hosts the PowerShell engine gets to decide how these commands will behave. The only way to change it is to modify the host application.

(FWIW, a lot of users don’t share your disappointment and actually asked for the v3 behavior… the input dialog was not universally loved. It is, the argument went, a command-line language, not a graphical tool.)
by GuyThomas at 2013-01-18 12:39:53
Thanks for your clear explaination. I noted the code you wrote to get the dialog box if required.

[quote][System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’) | Out-Null

$computer = [Microsoft.VisualBasic.Interaction]::InputBox(“Enter a computer name”, “Computer”, “$env:computername”) [/quote]