How to Use PowerShell Read-Host to Enter Multiple Lines?

In this tutorial, I will explain how to use PowerShell’s Read-Host cmdlet to capture multiple lines of input. As a developer, I often need to gather multiline input from users when processing data or configuring scripts. For instance, I recently had to collect a list of server names from a user for a deployment script. This tutorial will help you to use PowerShell Read-Host to enter multiple lines.

There will be many scenarios in which you might ask users to enter multiple lines. For example, you want to create a script that collects server names from a user to automate server maintenance tasks. The user needs to input multiple server names, one per line.

However, there is no direct approach to capture multiline inputs from the user using the Read-Host cmdlet, but you can use some indirect approaches.

Let me explain a few with examples.

Method 1: Using a Loop with Read-Host

One of the best approaches to capture multiline input is to use a loop that repeatedly calls Read-Host until the user signals they are done. Here’s an example:

$serverNames = @()
while ($true) {
    $input = Read-Host "Enter a server name (or type 'done' to finish)"
    if ($input -eq 'done') { break }
    $serverNames += $input
}
$serverNames

In this script, the user can enter server names one by one. Typing ‘done’ ends the input process, and the script collects all entered server names in an array.

I executed the complete script using VS code, and you can see the output in the screenshot below:

powershell read-host multiple lines

Check out the PowerShell read-host password

Method 2: Using a Multiline Input Box

You can create a graphical multiline input box using .NET classes for a more user-friendly approach. This method provides a text area where the user can input multiple lines at once.

However, this method required a little .NET framework knowledge. Here is the complete PowerShell script.

Add-Type -AssemblyName System.Windows.Forms
$form = New-Object System.Windows.Forms.Form
$form.Text = "Enter Server Names"
$form.Size = New-Object System.Drawing.Size(300,200)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Multiline = $true
$textBox.Size = New-Object System.Drawing.Size(260,120)
$textBox.Location = New-Object System.Drawing.Point(10,10)
$form.Controls.Add($textBox)

$okButton = New-Object System.Windows.Forms.Button
$okButton.Text = "OK"
$okButton.Location = New-Object System.Drawing.Point(100,140)
$form.Controls.Add($okButton)
$okButton.Add_Click({$form.Close()})

$form.ShowDialog() | Out-Null
$serverNames = $textBox.Lines
$serverNames

This script creates a simple form with a multiline text box. The user can enter multiple server names, each on a new line, and click the OK button to submit.

Check out How to Prompt for Yes/No Input in PowerShell Using Read-Host?

Method 3: Using Here-Strings

This is not a direct method to use read-host, but we can also use this method.

You can utilize here-strings, which allow you to define a multiline string directly within the script. This is useful for predefined multiline input or when reading from a file.

$serverNames = @"
Server1
Server2
Server3
"@ -split "`n"
$serverNames

In this example, the server names are defined within a here-string and then split into an array using the newline character.

Conclusion

There are no direct parameters available to capture multiline input by using the PowerShell Read-Host cmdlet. But you can use PowerShell Read-Host to enter multiple lines using different methods, such as a simple loop or a graphical input box using .Net.

I explained these methods with examples, and I hope this helps you.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.