Import User Accounts into Active Direcotry

Hello Experts,

I am wondering if anyone could help me with creating a powershell script. I think I can put together a PS script from videos and articles from the Internet but I'd like to add a few specific things to it:

Use a csv file to import user accounts into Active Directory.
Before the import happens, the script checks whether the account has already been created, if so, it skips the user account and process the next account.
After the import completes, the script deletes the file.

Thank you!
LVL 2
petaganayrAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Joshua GrantomSenior Systems AdministratorCommented:
What attributes are included in the csv file?

Are they the basics?

First Name, Middle Initial, Last Name, email?

Do you want to set a temporary password?
0
Will SzymkowskiSenior Solution ArchitectCommented:
This is one that i have created and i use all of the time...

Your CSV needs to have all of the heading that are illustrated before the command runs...
Name sn Country etc. If you do not want all of these field filled out just remove whatever ones you do not want from the script, and make sure that your CSV is constructed with the properties you want.

Import-Module activedirectory
$Userlist = Import-Csv "C:\CSV FILES\AD-Import-Updated.csv"

ForEach ($Person in $Userlist) {
$Person.Name
$Person.sn
$Person.Country
$Person.st
$Person.title
$Person.City
$Person.postalCode
$Person.telephoneNumber
$Person.Fax
$Person.givenName
$Person.displayName
$Person.department
$Person.company
$Person.SamAccountName
$Person.userPrincipalName
$Person.description
$Person.StreetAddress
$Person.Path
$Person.Password

New-ADUser -Name $Person.Name -Surname $Person.sn -Country $Person.Country -State $Person.st -Title $Person.title -City $Person.City -PostalCode $Person.postalCode -OfficePhone $Person.telephoneNumber -Fax $Person.Fax -GivenName $Person.givenName -DisplayName $Person.displayName -Department $Person.department -Company $Person.Company -SamAccountName $Person.SamAccountName -UserPrincipalName $Person.userPrincipalName -Description $Person.description -StreetAddress $Person.StreetAddress -Path $Person.Path -Enabled $true -AccountPassword (ConvertTo-SecureString $Person.Password -AsPlainText -force)
}

Open in new window


Will.
0
petaganayrAuthor Commented:
Joshua,

Just the basics - Firstname, LastName, Name.

Is there a way to import users without a password?

Thank you.
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

Joshua GrantomSenior Systems AdministratorCommented:
You can create users without a password. Another heads up is that it will create the user in the default Users OU unless you add another column named Path to your CSV with the Distinguished Name (ex. "OU=Users,OU=MySite,DC=mycomain,DC=com") and add this below.

-Path $user.Path `

Open in new window



Import-Module ActiveDirectory
$newusers = Import-CSV "C:\UserstoImport.csv"
ForEach ($user in $newusers) {
New-ADUser `
-SamAccountName $user.SamAccountName `
-Name ($user.FirstName + " " + $user.LastName) `
-DisplayName ($user.FirstName + " " + $user.LastName) `
-GivenName $user.FirstName `
-Surname $user.LastName `
}

Open in new window


If you want to test it out just add -WhatIf under the -Surname
0
Will SzymkowskiSenior Solution ArchitectCommented:
You have to be careful that if you do not specify an AccountPassword then the account will be disabled by default unless you specifically enable the account in the script.

Just remove what you do not need in my script and it will work without issue.

Will.
0
petaganayrAuthor Commented:
Thank you for the scripts I will try them soon, probably tonight.

I have a couple of stupid questions, I guess I could try these myself, but I am wondering if you guys know off the bat...

...can I specify 'AccountIsDisabled' False, create the script without a password, and still have the accounts enabled after being imported?

Also, can I exclude the column 'Path' from my csv file but have the DN path for the OU on the script? I am not sure if this makes sense, what I am trying to do here is minimize the number of columns that I will need to include on the csv file.

Thank you!!
0
Joshua GrantomSenior Systems AdministratorCommented:
You would use -Enabled $True and you can definitely have the OU in the script instead. I just put it in the CSV file incase you needed to create users in different OU's

Import-Module ActiveDirectory
$newusers = Import-CSV "C:\UserstoImport.csv"
ForEach ($user in $newusers) {
New-ADUser `
-SamAccountName $user.SamAccountName `
-Name ($user.FirstName + " " + $user.LastName) `
-DisplayName ($user.FirstName + " " + $user.LastName) `
-GivenName $user.FirstName `
-Surname $user.LastName `
-Enabled $true `
-Path "OU=Users,OU=MySite,DC=mycomain,DC=com"
}

Open in new window

0
Will SzymkowskiSenior Solution ArchitectCommented:
...can I specify 'AccountIsDisabled' False, create the script without a password, and still have the accounts enabled after being imported?
You would use the -Enabled $true to enable the accounts without a password.

Also, can I exclude the column 'Path' from my csv file but have the DN path for the OU on the script? I am not sure if this makes sense, what I am trying to do here is minimize the number of columns that I will need to include on the csv file.

Yes you can do this also. If you want all of the users to go into the same OU then you can specify the complete path and not use the $Path variable.

Will.
0
petaganayrAuthor Commented:
Here's what I came out with from combining both comments:

Import-Module activedirectory
$Userlist = Import-Csv "C:\Test2.csv"

ForEach ($Person in $Userlist) {
$Person.Name
$Person.gn
$Person.sn
$Person.displayName
$Person.userName

New-ADUser -Name $Person.Name -GivenName $Person.gn -Surname $Person.sn -DisplayName 

$Person.displayName -SamAccountName $Person.username -Path "OU=Domain 

Users,DC=source,DC=local" -UserPrincipalName ("{0}@{1}" -f $Person.userName,"source.local") 

-PasswordNotRequired $true -Enabled $true -ChangePasswordAtLogon $false
}

Exit

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
petaganayrAuthor Commented:
: D
0
petaganayrAuthor Commented:
Joshua and Will, thank you for the help!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.