Copy AD group members from one to another

Welcome Forums General PowerShell Q&A Copy AD group members from one to another

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

 
Participant
2 weeks, 3 days ago.

  • Author
    Posts
  • #174280

    Participant
    Topics: 3
    Replies: 8
    Points: 57
    Rank: Member

    Hello,

    I have a command to copy AD group members from one to another
    but i want to do that with the help of Excel file for multiple groups.
    i want PowerShell to get the content of column A (old group name) and copy all the members to the new group (column B).
    internet search didn't help for me.

    Thanks.

  • #174298

    Participant
    Topics: 1
    Replies: 1530
    Points: 2,591
    Helping Hand
    Rank: Community Hero

    We're used to be better when we get something to tweak. What do you have so far? Please show your code. Powershell.org is not a free code writing service – you know that, right? We expect you to do your own research before and at least try to solve your problem by yourself.

  • #174316

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    Atx750K,

    Olaf is right without having any code to work with we can't really assist much further. Below is an example of how I would go about it. Note the Headers in the csv file would be OldGroup and NewGroup.

    ForEach ($Group in (Import-Csv C:\Temp\GroupAssignment.csv)){
       $OldGroupMembership = Get-ADGroupMembership -Identity $Group.OldGroup
       If ($null -ne $OldGroupmembership){
          Add-ADGroupMembership -Identity $Group.NewGroup -Members $OldeGroupMembership
       }
    }
  • #174478

    Participant
    Topics: 3
    Replies: 8
    Points: 57
    Rank: Member

    Hello,

    Thanks for your replay

    i have this so far:

    Add-ADGroupMember -Identity 'New Group' -Members (Get-ADGroupMember -Identity 'Old Group' -Recursive)

     

  • #174481

    Participant
    Topics: 3
    Replies: 8
    Points: 57
    Rank: Member

    Should i set a value for $Group?

  • #174595

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    Atx750K,

    When you use the foreach loop you have to assign a variable to the item that is being iterated through, it can be called anything you want. Here is another example.

    $Pizza = Get-Content C:\temp\Pepperoni.txt
    
    Foreach ($Slice in $Pizza){
       Write-Output "I ate $slice"}

     

     

  • #174616

    Participant
    Topics: 24
    Replies: 50
    Points: 84
    Rank: Member

    @jason.robertson as much as I prefer the spelling "olde", you do only use it one out of three times in your script. 🙂

    Add-ADGroupMembership -Identity $Group.NewGroup -Members $OldeGroupMembership

  • #174619

    Participant
    Topics: 0
    Replies: 100
    Points: 363
    Helping Hand
    Rank: Contributor

    John,

    Hahaha yeah, thankfully tab complete is our friend. 🙂 We can chalk it up to using mid 19th century English instead of modern English. 😉

     

You must be logged in to reply to this topic.