How to Copy Active Directory Groups from One User to Another with PowerShell

How to Copy Active Directory Groups from One User to Another with PowerShell

Creating new Active Directory users isn’t difficult, either with the GUI tools or PowerShell. But in large directories, creating consistent user objects can be an issue when the process is manual. With a little automation, you can make sure that new user objects are created with the right group memberships and attributes by copying them from a template account.

New-ADUser -Name 'Fleur Wade' -SamAccountName fleurwade -AccountPassword (ConvertTo-SecureString Pas$W0rd!!12 -AsPlainText -Force) -Enabled $true

I already have a template user account for users in the Accounts department. For more details on how I set up the accounts user template,

The next step is to enumerate the group membership of the template user (accountsuser) and then add Fleur’s account to the same Active Directory groups. We use Get-ADUser to enumerate which groups accountsuser belongs to. Select-Object is used to create a new object with all the information required by Add-ADGroupMember. Add-ADGroupMember is then used to add Fleur’s account to the same groups that accountuser is a member of.

Get-ADUser -Identity accountsuser -Properties memberof | Select-Object -ExpandProperty memberof | Add-ADGroupMember -Members fleurwade

My template account is a member of the ‘Accounts’ group. To check that Fleur’s account has added to the Accounts group, we can use Get-ADUser:

Get-ADUser -Identity fleurwade -Properties memberof

Automate the entire process

Let’s automate the process a bit more so that we only need input the new username and password, and PowerShell will handle the rest. The code below takes input from the console window for the username and password variables. Then it creates values for the givenName and surname parameters of the New-ADUser cmdlet by splitting the value of the $newUser variable.

We then create a variable ($newattributes), which copies the StreetAddress, City, Title, PostalCode, Office, Department, and Manager attributes from accountsuser. The New-ADUser cmdlet then runs to create a new user account based on the variables we defined in the previous section, copying the listed attributes from our template AD account to the new account.

Finally, we use Get-ADUser, Select-Object, and Add-ADGroupMember to add the new account to the same groups that accountsuser is a member of.

# Define variables
 
$newUser = Read-Host 'New user account (eg. Joe Blogs)'
$password = Read-Host 'Password (8-20 characters)'
$givenName = $newuser.split()[0]
$surname = $newuser.split()[1]
$newuserattributes = Get-ADUser -Identity accountsuser -Properties StreetAddress,City,Title,PostalCode,Office,Department,Manager
 
# Create a new user account
 
New-ADUser -Name $newUser -GivenName $givenName -Surname $surname -Instance $newuserattributes -DisplayName $newUser -AccountPassword (ConvertTo-SecureString $password -AsPlainText -Force) -ChangePasswordAtLogon $true -Enabled $true
 
# Copy group membership from template account
 
Get-ADUser -Identity accountsuser -Properties memberof | Select-Object -ExpandProperty memberof | Add-ADGroupMember -Members $newUser

And that is it! You can automate the process even further and change the above script to suit your needs. For example, you could modify the code so that the template account must be specified by the administrator in the console window. You could even create a script that sets up new accounts taken from a list or database.

Join the discussion