Hello,
I have a CSV file that contains a list of users we want to migrate onto a new environment.
In the file is the username, first name and last name.
Does anyone have a script that will import the username, first name and last name into a specific OU with our new AD structure. If possible set all the passwords too
Many Thanks!
9 Spice ups
There’s a good script on TechNet for this. Might need some modification depending on your needs, but the real meat and potatoes are there.
PowerShell: Create Active Directory Users Based On Excel Input
There is also Csvde which is designed for just this task.
This is one of the most common questions I’ve come across regarding powershell and you’ll likely find a million answers to this out on the web. However, every solution to this will need to meet your company’s needs and a script you downloaded may not. So you’ll need to understand a bit of the fundamentals of Powershell and Active Directory before you’ll be able to make this work for you.
Here are some great MS Virtual Academy courses that you can use to get started:
First steps:
Deeper:
Powershell and Active Directory:
7 Spice ups
This is essentially what a barebones script would look like. It collects your user info from a csv file and stores it in the $Users variable. Then it loops through each of these objects imported from Csv and creates the user based on the attributes provided. However, this is not really enough to make a useful user import script. For instance, you said you want to set the passwords for the users, but where will this information come from?
$Users = Import-Csv -Path c:\users\users.csv
$OU = 'ou=SpecificOU,dc=whatev,dc=com'
foreach ($User in $Users)
{
New-ADUser -Path $OU -GivenName $User.FirstName -Surname $User.LastName -SamAccountName $User.Username
}
3 Spice ups
mattmcnabb:
This is essentially what a barebones script would look like. It collects your user info from a csv file and stores it in the $Users variable. Then it loops through each of these objects imported from Csv and creates the user based on the attributes provided. However, this is not really enough to make a useful user import script. For instance, you said you want to set the passwords for the users, but where will this information come from?
$Users = Import-Csv -Path c:\users\users.csv
$OU = 'ou=SpecificOU,dc=whatev,dc=com'
foreach ($User in $Users)
{
New-ADUser -Path $OU -GivenName $User.FirstName -Surname $User.LastName -SamAccountName $User.Username
}
Matt, Great example. You should’ve also included an example of your CSV so that it’s clear that you MUST have fields in the CSV named “FirstName”, “LastName”, and “Username”, and of course any other fields that you might want to populate, like phone number and such.
1 Spice up
Thanks for all the help,
This is the script I am running:
But when I do I get this message asking me to put the full name in for each of the users in the CSV
Once I enter the name of the user it adds it to AD but i need to manually enter each on, is there a way to automate this process?
This is the layout of the CSV:
Cheers
‘Name’ is a required parameter for the New-ADUser cmdlet . You can reuse the Display Name here.
New-ADUser -Name $User.Fullname -Path $OU -GivenName $User.FirstName -Surname $user.LastName -SamAccountName $user.Username -DisplayName $user.Fullname
2 Spice ups