# Add ADComputers to ADGroups with CSV

**URL:** <https://forums.powershell.org/t/add-adcomputers-to-adgroups-with-csv/10259>\
**Category:** PowerShell Help\
**Created:** [March 7, 2018, 2:10pm UTC](https://forums.powershell.org/t/add-adcomputers-to-adgroups-with-csv/10259 "2018-03-07T14:10:34Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jdw23](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jdw23](https://forums.powershell.org/u/jdw23)\
**Post date:** [March 7, 2018, 2:10pm UTC](https://forums.powershell.org/t/add-adcomputers-to-adgroups-with-csv/10259/1 "2018-03-07T14:10:34Z")

</div>

I’m still new to powershell scripting. I have a csv file with a list of ad computers and the group each machine needs to be added to. I’m trying to build a script to get the adcomputer and add it to the adgroup listed next to each computer in the column.  
This is what I’m currently working with and it’s not working. I’m sure I’m confused at this point. Thank you in advance!

import-module activedirectory  
$groups = import-csv C:\filename.csv  
ForEach ($group in $groups) {  
Get-ADComputer -name $group.CLIENTNAME |  
}

---

<div class="post-metadata">

**Author:** ![greenfeet](https://avatars.discourse-cdn.com/v4/letter/g/b77776/32.png) [@greenfeet](https://forums.powershell.org/u/greenfeet)\
**Post date:** [March 7, 2018, 2:52pm UTC](https://forums.powershell.org/t/add-adcomputers-to-adgroups-with-csv/10259/2 "2018-03-07T14:52:13Z")

</div>

You can use the Add-ADGroupMember command to add computer to a security group.

```
ForEach($group in $groups) {
   Add-ADGroupMember -Identity ($group.groupName) -Members ($group.CLIENTNAME) 
}
```

---

<div class="post-metadata">

**Author:** ![jdw23](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jdw23](https://forums.powershell.org/u/jdw23)\
**Post date:** [March 7, 2018, 3:35pm UTC](https://forums.powershell.org/t/add-adcomputers-to-adgroups-with-csv/10259/3 "2018-03-07T15:35:36Z")

</div>

That makes sense! Let me try that and I’ll let you know. THANKS

---

<div class="post-metadata">

**Author:** ![jdw23](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jdw23](https://forums.powershell.org/u/jdw23)\
**Post date:** [March 7, 2018, 4:42pm UTC](https://forums.powershell.org/t/add-adcomputers-to-adgroups-with-csv/10259/4 "2018-03-07T16:42:26Z")

</div>

@Christian,

Thanks for the help, it worked. However, it seems to have found user accounts as well with some of the names in the list. Had to remove all the new group members. Do you know if I can make it more granular so it only looks for computer as the type?

---

<div class="post-metadata">

**Author:** ![postanote](https://avatars.discourse-cdn.com/v4/letter/p/ebca7d/32.png) [@postanote](https://forums.powershell.org/u/postanote)\
**Post date:** [March 7, 2018, 6:02pm UTC](https://forums.powershell.org/t/add-adcomputers-to-adgroups-with-csv/10259/5 "2018-03-07T18:02:39Z")

</div>

Get-ADGroupMember cmdlet has an object class property. Just filter for computers and use that for your Add on the pipeline.

Get-ADGroupMember -Identity ‘Administrators’ | Where-Object objectClass -eq ‘user’  
Get-ADGroupMember -Identity ‘Domain Controllers’ | Where-Object objectClass -eq ‘computer’

```
# Get parameters, examples, full and Online help for a cmdlet or function

(Get-Command -Name Get-ADGroupMember).Parameters
Get-help -Name Get-ADGroupMember -Examples
Get-help -Name Get-ADGroupMember -Full
Get-help -Name Get-ADGroupMember -Online

Get-Help about_*
Get-Help about_Functions

# Find all cmdlets / functions with a target parameter
Get-Help * -Parameter Append

# All Help topics locations
explorer "$pshome\$($Host.CurrentCulture.Name)"
```

---

<div class="post-metadata">

**Author:** ![aapeli-hietikko](https://avatars.discourse-cdn.com/v4/letter/a/47e85d/32.png) [@aapeli-hietikko](https://forums.powershell.org/u/aapeli-hietikko)\
**Post date:** [March 8, 2018, 1:39pm UTC](https://forums.powershell.org/t/add-adcomputers-to-adgroups-with-csv/10259/6 "2018-03-08T13:39:27Z")

</div>

How about like this. There is few extra steps, but you shouldn’t get too much red lines if the account does not exist 😉

```
ForEach($group in $groups) {
   
   $computer = $group.CLIENTNAME
   
   $ADComputer = get-adcomputer -filter {name -eq $computer} 
   
   if($ADComputer) {
        Add-ADPrincipalGroupMembership -Identity $ADComputer -MemberOf ($group.groupName) -Verbose
        }
   }
```

---

<div class="post-metadata">

**Author:** ![jdw23](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jdw23](https://forums.powershell.org/u/jdw23)\
**Post date:** [March 12, 2018, 4:50pm UTC](https://forums.powershell.org/t/add-adcomputers-to-adgroups-with-csv/10259/7 "2018-03-12T16:50:35Z")

</div>

This worked beautifully! Thank you very much for the help!

---

<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:36pm UTC](https://forums.powershell.org/t/add-adcomputers-to-adgroups-with-csv/10259/8 "2024-05-16T20:36:13Z")

</div>


