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:


# This script is used in a blog post on the https://mohitgoyal.co/2017/04/16/read-multi-line-input-from-users-in-powershell/. Please
# read the blog post for more information
function Read-MultiLineInputBoxDialog([string]$Message, [string]$WindowTitle, [string]$DefaultText)
{
Add-Type AssemblyName System.Drawing
Add-Type AssemblyName System.Windows.Forms
# Create the Label.
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Size(10,10)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.AutoSize = $true
$label.Text = $Message
# Create the TextBox used to capture the user's text.
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Size(10,40)
$textBox.Size = New-Object System.Drawing.Size(575,200)
$textBox.AcceptsReturn = $true
$textBox.AcceptsTab = $false
$textBox.Multiline = $true
$textBox.ScrollBars = 'Both'
$textBox.Text = $DefaultText
# Create the OK button.
$okButton = New-Object System.Windows.Forms.Button
$okButton.Location = New-Object System.Drawing.Size(415,250)
$okButton.Size = New-Object System.Drawing.Size(75,25)
$okButton.Text = "OK"
$okButton.Add_Click({ $form.Tag = $textBox.Text; $form.Close() })
# Create the Cancel button.
$cancelButton = New-Object System.Windows.Forms.Button
$cancelButton.Location = New-Object System.Drawing.Size(510,250)
$cancelButton.Size = New-Object System.Drawing.Size(75,25)
$cancelButton.Text = "Cancel"
$cancelButton.Add_Click({ $form.Tag = $null; $form.Close() })
# Create the form.
$form = New-Object System.Windows.Forms.Form
$form.Text = $WindowTitle
$form.Size = New-Object System.Drawing.Size(610,320)
$form.FormBorderStyle = 'FixedSingle'
$form.StartPosition = "CenterScreen"
$form.AutoSizeMode = 'GrowAndShrink'
$form.Topmost = $True
$form.AcceptButton = $okButton
$form.CancelButton = $cancelButton
$form.ShowInTaskbar = $true
# Add all of the controls to the form.
$form.Controls.Add($label)
$form.Controls.Add($textBox)
$form.Controls.Add($okButton)
$form.Controls.Add($cancelButton)
# Initialize and show the form.
$form.Add_Shown({$form.Activate()})
$form.ShowDialog() > $null # Trash the text of the button that was clicked.
# Return the text that the user entered.
return $form.Tag
}
$multiLineText = Read-MultiLineInputBoxDialog Message "Please enter some text. It can be multiple lines" WindowTitle "Multi Line Example" DefaultText "Enter some text here…"
if ($multiLineText -eq $null) { Write-Host "You clicked Cancel" }
else { Write-Host "You entered the following text: $multiLineText" }

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 !

One thought on “Read multi line input from users in PowerShell

  1. It seems that this method has a hard line limit of 350. That is, any multiline input that has more than 350 lines (or maybe < 32768 characters) will be truncated. Is there any way around this?

    Like

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 )

Facebook photo

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

Connecting to %s