Solved

Using Powershell to replicate group membership

Posted on 2016-09-19
4
41 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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

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 …
Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
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: …

706 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

20 Experts available now in Live!

Get 1:1 Help Now