# Script Code Help

**URL:** <https://forums.powershell.org/t/script-code-help/6771>\
**Category:** PowerShell Help\
**Created:** [July 7, 2016, 5:27am UTC](https://forums.powershell.org/t/script-code-help/6771 "2016-07-07T05:27:17Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dale-longstaff](https://avatars.discourse-cdn.com/v4/letter/d/278dde/32.png) [@dale-longstaff](https://forums.powershell.org/u/dale-longstaff)\
**Post date:** [July 7, 2016, 5:27am UTC](https://forums.powershell.org/t/script-code-help/6771/1 "2016-07-07T05:27:17Z")

</div>

Hello Guys,

I am aiming to add multiple users to an AD group and seen to be running into an error.

* * *

[System.Reflection.Assembly]::LoadWithPartialName(‘Microsoft.VisualBasic’) | Out-Null

$UserName = [Microsoft.VisualBasic.Interaction]::InputBox(“Enter usernames (seperated by a coma)”, “User”, “$env:username”)  
$ADGroupName = [Microsoft.VisualBasic.Interaction]::InputBox(“Enter name of AD Group”, “ADGroup”, “$env:ADGroup”)

$Users = $UserName.Split(“,”).Trim()  
ForEach ($user in $Users)  
{  
Add-ADGroupMember -Identity $ADGroupName -Members "$Users "  
}

* * *

I am getting the following error:  
Add-ADGroupMember : Cannot find an object with identity: ',longstaffd LAWS2 ’ under: ‘DC=user,DC=ad,DC=glasgow,DC=gov,DC=uk’.  
At line:12 char:6

- 

```
 Add-ADGroupMember -Identity $ADGroupName -Members ",$Users "

```

- 

```
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : ObjectNotFound: (,longstaffd LAWS2 :ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
  - FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

Add-ADGroupMember : Cannot find an object with identity: ',longstaffd LAWS2 ’ under: ‘DC=user,DC=ad,DC=glasgow,DC=gov,DC=uk’.  
At line:12 char:6

- 

```
 Add-ADGroupMember -Identity $ADGroupName -Members ",$Users "

```

- 

```
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : ObjectNotFound: (,longstaffd LAWS2 :ADPrincipal) [Add-ADGroupMember], ADIdentityNotFoundException
  - FullyQualifiedErrorId : SetADGroupMember.ValidateMembersParameter,Microsoft.ActiveDirectory.Management.Commands.AddADGroupMember

* * *

I imagine the issue is that the script is not adding in the coma to separate the users, any ideas?

Thanks.

\*\*\* EDIT \*\*\*

Sorry guys i managed to suss the issue after all.

Changed Users to Username in the last line of the code.

( Not sure how to delete the post)  
Thanks.

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [July 7, 2016, 9:48am UTC](https://forums.powershell.org/t/script-code-help/6771/2 "2016-07-07T09:48:56Z")

</div>

You did catch the main issue, which was referencing $Users rather than $User. Just keep in mind you should add some basic error handling to your code as well:

```
...
foreach ($user in $Users) {
    try {
        Add-ADGroupMember -Identity $ADGroupName -Members $User -ErrorAction Stop
    }
    catch {
        #Friendly error message
        "Unable to add user {0} to group {1}. {2}" -f $user, $ADGroupName, $_
    }
}
```

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:40pm UTC](https://forums.powershell.org/t/script-code-help/6771/3 "2024-05-16T20:40:55Z")

</div>


