Use PowerShell to Create a User in Active Directory

Summary: Use the Active Directory cmdlet New-ADUser to create a new user via Windows PowerShell.

Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife has the Window PowerShell Saturday site bookmarked on her Windows 7 smart phone, and she keeps hitting refresh on the little Internet Explorer window to watch the available tickets for the Charlotte PowerShell Saturday event march down to zero. I will admit, the tickets are going rapidly. This is a great chance to learn Windows PowerShell, meet new friends, and have a lot of fun—all for the nominal cost of lunch.

Use splatting to simplify user creation

The New-ADUser Windows PowerShell cmdlet (from the ActiveDirectory module) has a ton of parameters. Filling these out makes for cumbersome code, and does little to encourage readability. In fact, I hate having to read the command syntax (via Get-Help or Get-Command) because it seems to go on and on and on! I figure that most (if not all) of the attributes that are available to supply for a user in Active Directory can be supplied directly via a cmdlet parameter. (To be honest, I have never had the patience to compare the user schema with the cmdlet parameters to see if something is missing. By the way, anything that is missing can be filled in via the Set-ADUser cmdlet.

One way to make things a bit easier to read (and to open up interesting automation possibilities) is to use the technique of splatting. I have posted a number of Hey, Scripting Guy! Blogs that explain splatting or make use of the technique

Splatting uses a hash table

To use splatting, I create a hash table that has keys that are exactly the same as the names of the parameters of the cmdlet, and I store the hash table in a variable. I then assign values to the keys. These values are passed to the cmdlet when I pass the variable to the cmdlet that contains the hash table. The following command creates a hash table that contains a number of keys that are the same as the parameter names that are used by the Get-ADUser cmdlet.

$users = @{

 “name” = “fred”

 “givenName” = “manfred”

 “l” = “lexington”


When I run this bit of code, I see the hash table.

PS C:> $users = @{

>>  “name” = “fred”

>>  “givenName” = “manfred”

>>  “l” = “lexington”

>> }


PS C:> $users


Name                           Value

—-                           —–

l                              lexington

givenName                      manfred

name                           fred

To use this, all I do is import the ActiveDirectory module, create the hash table, and then pass the hash table to the Get-ADUser cmdlet. This code is shown here.

Import-Module activedirectory

$users = @{

 “name” = “fred”

 “givenName” = “manfred”

 “l” = “lexington”


New-ADUser @users

That is it. Now, all I need to do is to figure out a way to read a file of some sort, and automatically create the hash tables. Hmmmm…shouldn’t be too difficult, should it? After all, it is Windows PowerShell.  

Join me tomorrow for more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy