How to pass multiple variables in list to a single command

This topic contains 3 replies, has 4 voices, and was last updated by  Joel Sallow 4 weeks, 1 day ago.

  • Author
    Posts
  • #99457

    DeathStrock
    Participant

    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

  • #99477

    Nick Springer
    Participant

    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.

  • #99483

    PowershellNoob
    Participant

    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}
    }
    
  • #99508

    Joel Sallow
    Participant
    $Users | Get-MsolUser

    should work, I would think.

You must be logged in to reply to this topic.