• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 74
  • Last Modified:

Using Powershell to replicate group membership

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
ndalmolin_13
Asked:
ndalmolin_13
  • 2
1 Solution
 
footechCommented:
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
 
SubsunCommented:
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
 
footechCommented:
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
 
ndalmolin_13Author Commented:
This is exactly what I needed.
0

Featured Post

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now