Solved

Using Powershell to replicate group membership

Posted on 2016-09-19
4
48 Views
Last Modified: 2016-09-22
Hello Powershell Experts,

I have replicated my Active Directory environment in a test domain.  I have the same OU structure, the same users and the same groups.  The only thing left to do is replicate group membership.  My thought is to user powershell to export all of my groups and their members and then use Powershell to populate the groups in the test environment.  When I do a get-aduser, I don't see a memberof property.  What is the best way to do this?

Thanks,
Nick
0
Comment
Question by:ndalmolin_13
  • 2
4 Comments
 
LVL 39

Expert Comment

by:footech
ID: 41806376
Besides the default set, you need to specify any properties you want to retrieve using the -Properties parameter of Get-ADUser, so in this case you would specify the memberof property.  I would export using the Export-CliXml cmdlet, as the format is much more flexible in what it can contain compared to something like a .CSV.  Something like the below should work (not tested).
Get-ADUser -filter * -properties memberOf | Export-CliXml  users.xml
Import-CliXml users.xml | ForEach { Add-ADPrincipalGroupMembership -Identity $_.samAccountName -MemberOf $_.memberOf }

Open in new window

0
 
LVL 40

Accepted Solution

by:
Subsun earned 500 total points
ID: 41806424
This will work only if you have the same groups and users in domains, I mean then Samaccountname of objects should match.

To import group members..
Get-ADGroup -Filter * -ResultSetSize $null | %{$Group = $_.Samaccountname;$_ | Get-ADGroupMember | Select @{N="Group";E={$group}},Samaccountname} | Export-Csv C:\GroupList.csv -NTI

Open in new window

To Import..
Import-Csv C:\GroupList.csv | %{Add-ADGroupMember -Identity $_.Group -Member $_.Samaccountname}

Open in new window

0
 
LVL 39

Expert Comment

by:footech
ID: 41807190
Looking back, I think the method used by Subsun would be a bit better for the following reasons:
 - the memberOf property does not include the group set as Primary (though if the users are already created this may not be an issue)
 - what I posted would not handle nested group memberships

Combining the best of both (depending on the number of members in a group, this should also be more efficient as it adds all the members to a group in one call, instead of a separate call for each member).
# to export
Get-ADGroup -Filter * -Properties Members | Select DistinguishedName,Members | Export-Clixml groups.xml
# to import
Import-Clixml groups.xml | ForEach { $_ | Add-ADGroupMember -Members $_.Members }

Open in new window


* EDITED
0
 
LVL 1

Author Closing Comment

by:ndalmolin_13
ID: 41811053
This is exactly what I needed.
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
A procedure for exporting installed hotfix details of remote computers using powershell
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This is a video that shows how the OnPage alerts system integrates into ConnectWise, how a trigger is set, how a page is sent via the trigger, and how the SENT, DELIVERED, READ & REPLIED receipts get entered into the internal tab of the ConnectWise …

919 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now