In my previous post, I explained how to export a .CSV for each group in an OU. This gives you a .CSV for each group which is named the same as the group name.
- Some Group 1.csv
- Some Group 2.csv
- Some Group 3.csv
- Some Group N.csv
Now, I need to create those Groups in a new AD. Here is the script that I used:
Get-ChildItem | foreach { echo $_.Basename } | ForEach-Object { dsadd group -scope u "cn=$_,OU=Groups,OU=Some OU,DC=SubDomain,DC=Domain,DC=TLD" }
This script calls the program DSADD and passes it the correct name (minus the .csv) which is then used to create the Groups.
Next, we run a similar script that walks through those same .CSV's and adds the users to the group with the same name as the .CSV.
Get-ChildItem | foreach { $GN = $_.BaseName; Import-Csv $_ } | foreach { Add-ADGroupMember -Identity $GN -Members $_.SamAccountName }
This works wonderfully well if the column titled SamAccountName has the correct usernames in it.