0

I was wondering if someone could help me Importing users from a CSV or Excel file to AD based on a Templateuser. However i have no idea how to get the SamAccountNAme and Display name row for row from a CSV file so i don't have to manually change it.

$UserInstance = Get-ADuser -Identity "SaraDavis"
New-ADUser -SAMAccountName "EllenAdams" -Instance $userInstance -DisplayName "EllenAdams"

1 Answer 1

1

The CSV file would look something like this. Let's assume this information goes into c:\test\users.csv.

SamAccountName,DisplayName
trevor,Trevor Sullivan
nancy,Nancy Drew
billy,Billy Bob

The, script would look something like this:

# 1. Get a reference to the template user, with all properties
$TemplateUser = Get-ADUser -Identity TemplateUser -Properties *;

# 2. Import the CSV file
$Data = Import-Csv -Path c:\test\users.csv;

# 3. Create a new user for each row / item in the CSV file
foreach ($Item in $Data) {
    New-ADUser -Identity $Item.SamAccountName -DisplayName $Item.DisplayName -Parameter1 $TemplateUser.Property1 ... ... ... ...;
}
Sign up to request clarification or add additional context in comments.

5 Comments

thanks for your response. I tried executing the script below.... But for some reason it gives me the following error. I seperately ran the first line to check if it does find the user and it does execute it without a problem.
New-ADUser : A parameter cannot be found that matches parameter name 'Identity'. At C:\ScriptTest.ps1:4 char:25 + New-ADUser -Identity <<<< $Item.SamAccountName -DisplayName $Item.DisplayName -Parameter1 $TemplateUser 1; + CategoryInfo : InvalidArgument: (:) [New-ADUser], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.NewADUser
New-ADUser : A parameter cannot be found that matches parameter name 'Identity'. At C:\ScriptTest.ps1:4 char:25 + New-ADUser -Identity <<<< $Item.SamAccountName -DisplayName $Item.DisplayName -Parameter1 $TemplateUser 1; + CategoryInfo : InvalidArgument: (:) [New-ADUser], ParameterBindingException + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.NewADUser
Sorry, that was just supposed to be an example. It looks like New-ADUser has the -SamAccountName parameter.
would it be something like this? $TemplateUser = Get-ADUser -SamAccountName TemplateUser -Properties *; $Data = Import-Csv -Path c:\testlijst.csv; foreach ($Item in $Data) { New-ADUser -SamAccountName $Item.SamAccountName -DisplayName $Item.DisplayName -Parameter1 $TemplateUser.Property1; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.