• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 254
  • Last Modified:

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!
0
petaganayr
Asked:
petaganayr
  • 5
  • 3
  • 3
3 Solutions
 
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
WEBINAR: 10 Easy Ways to Lose a Password

Join us on June 27th at 8 am PDT to learn about the methods that hackers use to lift real, working credentials from even the most security-savvy employees. We'll cover the importance of multi-factor authentication and how these solutions can better protect your business!

 
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
 
petaganayrAuthor Commented:
: D
0
 
petaganayrAuthor Commented:
Joshua and Will, thank you for the help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

  • 5
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now