Add ADComputers to ADGroups with CSV

Welcome Forums General PowerShell Q&A Add ADComputers to ADGroups with CSV

This topic contains 6 replies, has 4 voices, and was last updated by

 
Participant
8 months ago.

  • Author
    Posts
  • #95376

    Participant
    Points: 0
    Rank: Member

    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 |
    }

  • #95386

    Participant
    Points: -19
    Rank: Member

    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) 
    }
    
    • #95391

      Participant
      Points: 0
      Rank: Member

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

    • #95400

      Participant
      Points: 0
      Rank: Member

      @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?

    • #95460

      Participant
      Points: 1
      Rank: Member

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

      Participant
      Points: 0
      Rank: Member

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

  • #95407

    Participant
    Points: 206
    Helping Hand
    Rank: Participant

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

The topic ‘Add ADComputers to ADGroups with CSV’ is closed to new replies.