Add ADComputers to ADGroups with CSV

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 |

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) 

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


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?

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)"

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

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

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