Solved

Using Powershell to replicate group membership

Posted on 2016-09-19
4
59 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

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 40

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

Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

696 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