Aksanya
Aksanya

Reputation: 3

Script not getting multiline input

I have the following script that allows me to get user IDs on the console and then check expiration dates of relevant users' AD accounts. The part gathering the IDs is as below:

[array]$input = (Read-Host “Staff Numbers: ”).Split([System.Environment]::NewLine, [StringSplitOptions]::RemoveEmptyEntries) 

However when I paste the staff numbers of the user to the console, it reads only the 1st line and ends the script. The other numbers after the first are just displayed on the console after.

Any idea on what is wrong?

Upvotes: 0

Views: 101

Answers (1)

Robert Dyjas
Robert Dyjas

Reputation: 5227

Read-Host is supposed to take only one line, as per the docs:

The Read-Host cmdlet reads a line of input from the console.

If you want to get multiple lines you can either:

  • import data from file
  • use Read-Host inside a loop like here
    $arr = @(While($line=(Read-Host).Trim()){$line})
    
  • separate values by any other character (comma, semicolon etc.) as suggested on /r/PowerShell
    $userInput = read-host "Enter a list of comma separated hostnames"
    $data = $userInput.split(",").trim(" ")
    

Upvotes: 1

Related Questions