?
Solved

Create users in AD from CSV using powershell

Posted on 2011-04-25
9
Medium Priority
?
943 Views
Last Modified: 2012-05-11
I have a CSV with hundreds of users that I want to create in AD.  The CSV is like this:

Employee      Name      Email      Mobile      Business-card-phone
firstname      lastname      firstname@company.com      123-456-789      123-456-789

I can create users on the domain using powershell and I'd prever to use the quest AD powershell plugin qad-user cmdlet, but I'm having trouble scripting it.
0
Comment
Question by:LrdKanien
  • 4
  • 3
  • 2
9 Comments
 
LVL 15

Expert Comment

by:JBond2010
ID: 35463277
Have a look at the sample below.

$objOU=[ADSI]"LDAP://OU=Company, DC=domain, DC=local"
$objUser=$objou.Create("user"CN=Joe Bloggs")
$objuser.Put("sAMAccountName","joe.bloggs")
$objuser.SetInfo()

Powershell and VBscripting are more or less the same. Except VBscripting is more powerful as it has being around longer and there is more langauge.
0
 
LVL 43

Accepted Solution

by:
Adam Brown earned 1500 total points
ID: 35463291
You would do something like the attached. It would change a bit depending on how you want your usernames to work. You would need to either expand your CSV to add in usernames for the samaccountname attribute or you can use an alogrithm of some type to generate one from the CSV you have. The -parentcontainer switch tells powershell where to place the new user and apparently requires DN notation.
import-csv <csv file> | foreach {$first,$last,$email,$mobile,$bcard = $_.firstname,$_.lastname,$_.email,$_.mobile,$_.business-card-phone
new-qaduser -firstname $first -lastname $last -displayname "$first $last" -mobilephone $mobile -phonenumber $bcard -name "$first $last" -parentcontainer <DN of the OU for the users to be placed in> -samaccountname <username>}

Open in new window

0
 
LVL 43

Expert Comment

by:Adam Brown
ID: 35463330
JBond, VB and Powershell are quite different, and VB is very likely going to go the way of the Dodo in the future. For the most part, Powershell is much easier to script with and often requires fewer lines of code to do the same things, in addition to the fact that you don't need to learn a programming language to actually make good use of it. It helps to learn some .NET to do some of the more advanced stuff, but it isn't completely necessary to learn complicated syntax. You just need to learn how to handle cmdlets. There are also a lot of good arguments to back up the idea that Powershell is *WAY* more powerful than VB script. In particular the Remote powershell capabilities of Powershell 2 go way beyond anything you can do with VBS.
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
LVL 15

Expert Comment

by:JBond2010
ID: 35463402
@ acbrown, I agree with what your saying. But vbscript does interact with .NET, but it does not fully use .NET the same way Powershell does. Powershell is dependent on .NET.

Thanks for the info.


JBond2010
0
 

Author Comment

by:LrdKanien
ID: 35463556
do you know how to make the samaccountname equal to the prefix of their email address? (before the @)
0
 
LVL 43

Expert Comment

by:Adam Brown
ID: 35463571
if you want the username (the name they use when they log in) to be just their first name, you would use -samaccountname $first
You could use -samaccountname "$first.$last" for it to be firstname.lastname, and a few other variations. There are some additional lines required if you wanted something like firstnamelastinitial, though.
0
 

Author Comment

by:LrdKanien
ID: 35463586
no need.  I created them myself.  I now have a category "username" at the end.
0
 
LVL 43

Expert Comment

by:Adam Brown
ID: 35463592
You could also use the split function to create an array for the email account by doing this:
$username = $email.Split("@")

This would go between the two lines of the script above. At that point you would use -samaccountname $username[0] in the second line of the script.
0
 

Author Closing Comment

by:LrdKanien
ID: 35471396
code was a bit backwards with the array mappings compared to my example and the switches had to be in a different order in for the cmdlet to function
0

Featured Post

Making Bulk Changes to Active Directory

Watch this video to see how easy it is to make mass changes to Active Directory from an external text file without using complicated scripts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
Transferring FSMO roles is done when an admin wants to split roles between certain Domain Controllers or the Domain Controller holding the Roles has been forcefully demoted using dcpromo / forceremoval
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question