My mission is to create new user accounts from data held in a CSV file.
For this mission we need the following skills, execute PowerShell cmdlets,
install QAD snap-ins,
research LDAP properties and control spreadsheet columns.
Whenever I plan a script which imports users into Active Directory,
the very best preparation is to create a script which
exports user accounts
from the domain. The benefit is that we learn the syntax and
develop strategies in the knowledge that we are not flooding Active Directory with
useless accounts.
While the QAD commands simplify the task of creating new users, it's
worthwhile spending time investigating the get-QADUser cmdlet before turning our
attention to the more powerful new-QADUser.
Pre-requisites, particularly for the QAD snap-in
Before we can get my examples to work you need to meet these
pre-requisites.
Download and install PowerShell and .Net Framework.
Go to
Microsoft's site and choose the flavour to suit your operating system.
Let us investigate which parameters are required by the main, import-Csv cmdlet.
# Research PowerShell's import-Csv cmdlet
get-Help import-Csv -full
Note 1: The most important conclusion of this research is that
import-Csv requires a path. When we employ this cmdlet for real, we must include
a parameter to tell PowerShell where to find the .csv file.
This is an intermediate stage designed to build confidence and to gain a feel
of QAD syntax.
Before you begin, it's imperative that you find the variable $OU in my script(s), then amend its value to reflect
your domain, and your Organizational Unit. You many need a little
extra work with Active Directory Users and Computers in creating an OU and a handful of users.
QADUser: List Accounts in a Named OU
# PowerShell script to list
Active Directory users in a named OU # Author: Guy Thomas # Version 1.2
August 2008 tested on PowerShell v 1.0
Note 1: The crucial piece of information
that
new-QADUser needs is: 'Where are the users going to be created?'
The answer is held by the very first parameter -ParentContainer.
In the main script we will use $OU to hold the value of your domain
and your Organizational Unit.
Note 2: -Name, well every object needs a name.
But after -Name comes a plethora of other parameters which look like
like LDAP properties. This is a list to research when you
need to populate the user accounts with optional properties such as 'Manager',
'Company' or 'DisplayName'.
Conclusion: I will produce a short
list of the minimal properties that you need to create your first
tranche of new users. Let us start with just -name and -sAMAccountName.
Once that works, we will be more ambitions and introduce properties
such as: -lastName -firstName, -userPrincipalName and -company.
We are going to store the user's data in a spreadsheet. The
first row is crucial because here are the names of the properties. To
keep it simple and logical I will name them after the properties that we
researched for new-QADUser, for example, name and sAMAccountName.
Each row in the spreadsheet holds the values for one user. The
final job for the spreadsheet is to save as a .csv file - and to
remember that file path.
Name
sAMAccountName
Bush
gbush
Obama
bobama
Clinton
hclinton
Note 1: The first row holds the vital property names. You could think
of these 'fields', as LDAP properties, or import-Csv parameters.
In the master plan we will employ the import-Csv cmdlet to read the
spreadsheet. Then we will feed in the values held in the columns,
so that they become the properties of the
new-QADUser.
Preparation: Edit $OuBorn. The value of 'OU=PowerShell,DC=cp2,DC=mosel'
will not work with your domain.
Where is the path to your spreadsheet? Edit $Freshmen.
# PowerShell script create users in a named OU # Author: Guy
Thomas # Version 2.4 August 2008 tested on PowerShell v 1.0
Note 1: The two crucial cmdlets are:
import-Csv and new-QADUser. Observe how PowerShell pipes the
output of import-Csv into new-QADUser. Incidentally, (|) is an
important method in PowerShell, learn it here and employ this pipe
in future scripts.
Note 2: I have introduced two variables, $OuBorn
and $Freshmen. To be brutally honest, the script will not work
unless you change the values held by these $Variables to reflect your environment.
Note 3: $_. is a special PowerShell variable
which says, 'In this stream', hence $_.name means use the name
column from the spreadsheet. I hope that you can now see the
importance of the first row in your .csv file.
Note 4: Observe the backtick (`).
Perhaps the best way of seeing what this word-wrap symbol achieves is to
remove it, and type the command all on one line:
My speciality is getting you started. Once you can create a
basic user, then it will be a labour of love to research more properties
to include in your spreadsheet. For example, if you have a
real-life task of creating dozens of users, then adding values for
LastName, and UserPrincipalName would result in better users!
Two more things that you might like to do with your users
Tip: Try an
Export-Csv
for a user that you created manually in Active Directory User and
Computers. Then research the LDAP names in the first row.
Naturally, the more property boxes that you add information, the
more sense that you can make of the resulting export.
In order to import user
accounts into active directory you need to be multi-talented. In addition to PowerShell skills, you need
to be a veteran of spreadsheets and a professor of LDAP properties. What
makes a difficult task achievable is the QADUser object which you obtain with
the QAD snap-in.
Please write in if you see errors of any kind. Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.
Guy
Recommends: Orion's NPM - Network Performance Monitor
Orion's performance monitor is designed for detecting network outages.
A network-centric
view make it easy to see what's working, and what needs your attention.
This utility guides you through troubleshooting by indicating whether the
root cause is faulty equipment or resource overload.