PowerShell – Read-Host values into an Array

I was recently working on a PS script and I wanted to use read-host values into an array. Here is my solution:

$accounts = (Read-Host "user accounts (separate with comma)").split(',') | ForEach-Object {$_.trim()}
foreach ($account in $accounts)
{
get-aduser $account | select name
}

The .split() method is self explanatory, the .trim() method will simply remove leading and trailing spaces and leading and trailing line breaks

Leave a Reply

Your email address will not be published. Required fields are marked *

2 + 2 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.