Read multi line input from users in PowerShell

As you are aware, Read-Host cmdlet can be used to read single line input from users in PowerShell. But there is no out-of-the-box cmdlet to read multi line input from users. We can achieve this in a roundabout way:

 $inputFromUser = @()
 $input = ''
 While($input -ne "q") {
 If ($input -ne $null) {
 $InputFromUser += $input.Trim()
 }
 $input = Read-Host "Enter the input here (Enter 'q' to exit) "
 }
 $inputFromUser = $inputFromUser[1..($inputFromUser.Length-1)]

Below’s is a sample run from my machine:

multi-line-input-demo
Multi Line Input using Read-Host

We can also do this using GUI in PowerShell. For that, we have to import some .NET based assemblies related to GUI. The following code is based on this Technet article:

Below is a sample run from my machine:

multi-line-input-demo-using-gui
Multi Line Input in PowerShell using GUI

multi-line-input-demo-using-gui-2

Give it a try by yourself !

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s