Multiple Parameter Entries Issue

Wondering if anyone can help with this issue please.

I’m running the following script to check whether a user exists in an AD Group. If they do, return a message saying that they already exist in the AD Group. If they don’t, add them to the AD Group and return a message to say that they have been added.







$GroupMemberUsernames = (Get-ADGroupMember -Identity "ADGroupX" -recursive | get-aduser -properties SamAccountName).SamAccountName

    If ($GroupMemberUsernames -contains $Username) {

      Write-Host "$Username already exists in ADGroupX"


    Else {

      Add-ADGroupMember -id "ADGroupX" -members $Username

      Write-Host "$Username has been added to ADGroupX"


I am being prompted for multiple usernames one after the other until I leave one blank, at which point the search runs. This is what I wanted because it means that I can perform the script for multiple users and is why I used parameters instead of read-host.

The script works to a point in that it adds multiple users to ADGroupX if they aren’t already in it.

Unfortunately, it seems to run the search for the multiple users at the same time rather than individually. i.e. when I run the script for UserX and User Y, the output shows:

‘UserX UserY has been added to ADGroupX’

rather than:

'UserX has been added to ADGroupX

UserY has been added to ADGroupX’

This also means that I never get a message saying that ‘UserX already exists in ADGroupX’ even when they do. I think because it is looking for ‘User X User Y’ in ADGroup X rather than just User X, then just User Y?


When you assign multiple values to $Username you are creating an array. When you call the $Username variable later (e.g. line 16, 18, 24, 26), you are calling the entire array, rather than the values (usernames) one at a time.

Probably the easiest way for you to accomplish what you want is to loop through the names in your array with foreach so that they can be checked one at a time.

Thanks for your help grokkit