How to Copy Active Directory Users with PowerShell

Creating new Active Directory (AD) users with PowerShell is easy using the New-ADUser cmdlet. But there are a lot of attributes that you might need to set every time you set up a new user account in AD. For example, you might want to set the user’s office address, department, job title, and other details. With the help of the -Instance parameter and the New-ADUser cmdlet, you can copy AD attributes from an existing user when creating new accounts.

Create a template account

Let’s start by creating a user account, called ‘Accounts User’, that has all the attributes set for subsequent users. I’m going to set the following attributes:

  • StreetAddress
  • City
  • Title
  • PostalCode
  • Office
  • Department
  • Manager

Even though ‘Accounts User’ will just act as a template for new users, we need to set a password so that the account meets the password policy requirements of AD. Additionally, New-ADUser requires the password to be passed as a secure string, so we’ll use ConvertTo-SecureString to convert the plaintext password.

Copy account attributes using the -Instance parameter

Now that the template user has been created, we should create a variable ($newuserattributes) containing an object that represents the template user account (Accounts User) with only the attributes we want to copy. Some attributes, like badPwdCount and lastLogon, cannot be copied. So, specifying only the attributes we need ensures PowerShell won’t throw an error.


Let’s create a new user account and copy attributes from ‘Accounts User’. The new user is called Fleur Wade and we need to make sure we specify values for the -Name, -GivenName, SurName, and -AccountPassword parameters. The -Instance parameter is used to specify from which account we will copy other parameters, like StreetAddress and City. I’ve also set -ChangePasswordAtLogon and -Enabled to $true to make sure the account is ready to use straightaway.

Add the new user to the Accounts group

New-ADUser cannot be used to set AD group membership. So, as part of the process, I’m also going to configure group membership for the new account using Add-ADGroupMember.

Verify user attributes and group membership

Finally, we can use Get-ADUser and Get-ADGroupMember to check the new account was configured correctly with attribute values copied from ‘Accounts User’, and that Fleur’s group membership was also set properly.

Image # Expand
How to Copy Active Directory Users with PowerShell (Image Credit: Russell Smith)

Creating new Active Directory users in bulk

The above method can help you to create new Active Directory users faster than manual methods, like using the GUI management tools. If you’d like to create new user accounts with information pulled from a data source, like an Excel spreadsheet, PowerShell can help achieve that. Check out Create New Active Directory Users with Excel and PowerShell on Petri for complete instructions.

Related Topics:

  • Active Directory
  • BECOME A PETRI MEMBER:

    Don't have a login but want to join the conversation? Sign up for a Petri Account

    Register
    IT consultant, Contributing Editor @PetriFeed, and trainer @Pluralsight. All about Microsoft, Office 365, Azure, and Windows Server.