Author Posts

April 23, 2018 at 7:04 am

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:
User_abc
User_def
User_hij

April 23, 2018 at 1:40 pm

Try flipping the flow and using a foreach

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

VS

$Users|foreach{Get-MsolUser $_} 

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

April 23, 2018 at 2:19 pm

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.

[Cmdletbinding()]
Param
(
    [Parameter(Mandatory=$True)]
    [String[]]$Users
)

foreach($User in $Users)
{
    Get-ADUser -filter {DisplayName -eq $User}
}

April 23, 2018 at 3:55 pm

$Users | Get-MsolUser

should work, I would think.