Solved

Using Powershell to replicate group membership

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CSV file manipulation 6 26
Recycle MSExchnge powershell app Pool 2 17
Power shell Script - Help v2 7 21
write-output without carrier return 1 16
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

778 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