How to pass multiple variables in list to a single command

Hello powershell experts,
I have a question, I want to take multiple variables in a list and pass those variables in list to a single command. And i want that command to work for each variable in link.

Here’s what i did, but that clearly don’t work.
Write-Host “enter Users you want to search”
$User1 = Read-Host "user1 = "
$User2 = Read-Host "user2 = "
$User3 = Read-Host "user3 = "
$Users = @($User1, $User2, $User3)

    Get-MsolUser | Where-Object DisplayName -eq  $Users

I wan’t above command to show users as follow:

Try flipping the flow and using a foreach

Get-MsolUser | Where-Object DisplayName -eq $Users


$Users|foreach{Get-MsolUser $_} 

Now I tested this with the Get-ADuser cmdlet so the syntax may not be correct for the MSOLUser cmdlet.

Also tested with Get-Aduser, would something like this work for you?

You could just make the parameters Mandatory instead of using Read-Host for each.


foreach($User in $Users)
    Get-ADUser -filter {DisplayName -eq $User}
$Users | Get-MsolUser
should work, I would think.