-1

I am trying to import some AD Users using a powershell script and a file.csv but I am getting this error:

New-AdUser : The specified account already exists

Here is the script:

Import-Module ActiveDirectory 
$Users = Import-Csv -Delimiter ";" -Path ".\userslist.csv"  
foreach ($User in $Users)  
{  
    $OU = "OU=pfsense,DC=tech,DC=local"  
    $Password = $User.password 
    $Detailedname = $User.firstname + " " + $User.name 
    $UserFirstname = $User.Firstname 
    $FirstLetterFirstname = $UserFirstname.substring(0,1) 
    $SAM =  $FirstLetterFirstname + $User.name 
    New-ADUser -Name $Detailedname -SamAccountName $SAM -UserPrincipalName $SAM -DisplayName $Detailedname -GivenName $user.firstname -Surname $user.name -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path $OU  
} 

Here is the CSV file:

Name;Firstname;Password
MILLE;Thierry;P@$$w0rd!1
MILLE;Vendelin;P@$$w0rd!2
MILLE;Blandine;P@$$w0rd!3

What should I do to fix this?

7
  • Find and delete or change the existing account. The error is telling you that this person is ALREADY IN Active Directory.
    – Joel Coel
    Oct 7, 2015 at 19:25
  • no the active Directory was clean when I was trying to run this script.
    – Ali
    Oct 7, 2015 at 19:27
  • 4
    Check it again. The error clearly says "The specified account already exists". Look in a different OU. User objects must be unique across the domain, not just within an OU.
    – Joel Coel
    Oct 7, 2015 at 19:37
  • That $SAM doesn't really look like a UPN to me. Dunno if that matters though, AD might be able to handle it. Have no way of trying it myself at the moment.
    – notjustme
    Oct 7, 2015 at 20:09
  • I also think you should fix the value for upn to someting like "$($SAM)@domain.local". When you run this script, is any user created or none? Oct 12, 2015 at 11:03

1 Answer 1

0

Looking closer at your script, it looks like you might have fat fingered the input for $FirstLetterFirstname. You'll want to add the dot in for $User**[dot]**Firstname.substring(0,1)

$FirstLetterFirstname = $User.Firstname.substring(0,1) 

You might also want to add your domain to the UserPrincipalName

-UserPrincipalName $SAM + "@EXAMPLE.com"
1
  • This is happening while the users are successfully been imported to the OU so the script is not failing but it just displays that red error massage during the import.
    – Ali
    Oct 7, 2015 at 19:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .