In my last article I discussed a few techniques for exporting user accounts from Active Directory using PowerShell. The obvious next step is to see how to import user accounts into Active Directory.
Profile gravatar of Jeffery Hicks

Jeffery Hicks

Jeffery Hicks is a multi-year Microsoft MVP in Windows PowerShell, Microsoft Certified Professional and an IT veteran with 25 years of experience specializing in automation. He works today as an author, trainer and consultant.
Profile gravatar of Jeffery Hicks

I am going to use the New-ADUser cmdlet from the Microsoft Active Directory provider. As you’ll see this can be a simple and seamless process and it doesn’t really matter if you are importing from a CSV file or XML. What matters is the object.

When you look at help for New-ADUser you’ll see that almost all of the parameters accept pipeline input by property name. That means if the incoming object has a property, like Title, that matches the parameter name, the cmdlet will use that property value for the parameter value.

The only other step you will most likely have to take is to define an initial password for the user account. You can create a user without specifying a password but that seems like extra work to me. The minor obstacle is that New-ADUser needs a secure string for the password property. So I’ll define one.

Use –Force to suppress the confirmation prompt. As an alternative, you can use Read-Host and prompt for the password.

I’ll show you where to use this and how to force the user to change password. Now we can begin.

Importing objects ^

PowerShell can import any CSV file. It will use the header for property names.

Import CSV with PowerShell

Import CSV with PowerShell

The property names (i.e. the CSV header) for the most part match the parameter names for New-ADUser. This means I could get by with something as simple as this:

The only parameters I had to specify was the path for the new accounts, password parameters and something to enable the account. Even though I am using a CSV file here, I could just as easily have used an XML file or document. All that matters is that the objects you get when importing match up.

But let me take this a step further. Suppose I need each user to be placed in the OU that corresponds to their department. I also want to define a few other properties that aren’t part of the original, imported user object.

As each user is imported, I use custom hash tables to define some additional properties that will bind to New-ADUser by property name. I’ve moved my other parameters like Enabled into the object and added some additional ones for description. The OtherAttributes will set the Info (or Note) property so I can record when the account what created and by whom. For some reason, and I assume it must be a bug, I couldn’t define OtherAttributes as part of the incoming object so I used it as a regular parameter. But it works.

When properties don’t line up ^

Unfortunately you may not always get perfectly formatted CSV or XML files. Perhaps your CSV file only gives you something like in the screenshot below.

 Property names don’t match

Property names don’t match

The property names don’t match and keys like samAccountname and UserPrincipalName are missing. Here’s one way. For this example, I’m going to import all of the accounts into the same location.

I will need to process each user account to come up with properties I can pass to New-ADUser.

For each user in the collection of imported users I’m creating a hashtable. Each key corresponds to a parameter for New-ADuser. Some of the properties, like Title, I can simply use the existing value. But others, like samAccountname, I construct on the fly based on existing properties. This hash table will be splatted to New-ADUser so that each key/value pair lines up with the corresponding parameter. In literally a second, I’ve created 10 populated Active Directory user accounts.

Summary ^

Importing account data from a CSV or XML file into Active Directory will make you look like a hero. What could take hours to accomplish manually can be achieved in literally seconds. You can use the same principles and techniques I’ve shown here to modify or even remote user accounts.

Take part in our competition and win $100!

Share
0

Related Posts

4 Comments
  1. avatar
    Pat Canterino 1 year ago

    Can the exported file be modified before it is imported?  Does it matter if it is from another domain/network?

    0
    • avatar
      Robert Löfgren 1 year ago

      Pat, you can import / export between networks and domains without any issue.

      Also, you can modify the .csv file in Excel (or notepad) before importing.

      0
  2. avatar
    MIke 11 months ago

    What about unix attributes? I've tried using this process, but it ignores uidNumber, gidNumber, unixHomeDirectory, etc when the users get imported to the new domain. I can see those fields in ADSI edit, but New-ADUser doesn't want to set those.

    It seems that Set-ADUser will let you  modify the unix attributes. But is there any way to set them with New-ADUser? Adding all necessary attributes at once with New-ADUser would greatly simplify things.

    0
  3. avatar
    dharmatkj 8 months ago

    My file csv can you help to import my file.pliss source code

    IMAGES FILE CSV

    0

Leave a reply

Your email address will not be published. Required fields are marked *

*

CONTACT US

Please ask IT administration questions in the forum. Any other messages are welcome.

Sending
© 4sysops 2006 - 2017

Log in with your credentials

or    

Forgot your details?

Create Account