computer to multiple AD Groups

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Peter Jurgens Peter Jurgens 3 weeks, 6 days ago.

  • Author
    Posts
  • #69427
    Profile photo of Tony Rodal
    Tony Rodal
    Participant

    Hi, I see a lot of examples of adding computers from a list to an AD Group. Does someone have an example of adding multiple AD Groups from a text file to a computer?

  • #69432
    Profile photo of Paul DeArment Jr
    Paul DeArment Jr
    Participant

    Assuming you have a csv file you can do something like the following

    $computer = get-adcomputer "TestServer"
    import-csv "C:\groupsExample.csv")| foreach-object{add-adgroupmember -identity $_.group -members $computer }
    

    if you have a text file without a column header and can't use import-csv you can modify this slightly to get-content

    $computer = get-adcomputer "TestServer"
    get-content "C:\groupsExample.txt" | foreach-object{add-adgroupmember -identity $_ -members $computer}
    
    • #69519
      Profile photo of Peter Jurgens
      Peter Jurgens
      Participant

      I find the *-ADPrincipalGroup[membership] cmdlets are lesser known but could help avoid having to use foreach in this situation.

      Add-ADPrincipalGroupMembership -Identity PC001 -MemberOf $(Get-Content .\groups.txt)
      
  • #69468
    Profile photo of Tony Rodal
    Tony Rodal
    Participant

    Thanks for the help.

You must be logged in to reply to this topic.