Sunday, September 28, 2014

Powershell - Copy Group Membership

The other day one of the guys on the helpdesk was tasked with creating a new distribution group that would mirror the membership of an existing group. This normally isn't a problem if the group has 5-10 people but the group he had to copy had over 200 members. As soon as I heard that I knew Powershell was the answer. It took me maybe 5 minutes to write this script and saved a whole lot of time.

I do want to add that we spent an extra 20 minutes troubleshooting because we kept receiving access denied errors. It turns out that when he created the group he added his account (not domain admin) in the managed by field. Running the script under my DA account kept giving me those permission errors. The script only worked after removing his account from the managed by field. I was bit blindsided by that but overall a good learning experience. Anyways below is the script I used.

 $sourceGroup = "SourceGroup Name"  
 $targetGroup = "TargetGroup Name"  
 Get-ADGroupMember -Identity $sourceGroup | foreach {$._SamAccountName} { $varName = $_.SamAccountName ; Add-ADGroupMember -Identity $targetGroup -Members $varName }  

I'm thinking about developing this script so that it can ask for feedback instead of editing the script directly. It would make it friendly to anybody who isn't used to scripting. Stay tuned!

No comments:

Post a Comment