-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?

Nixphoe
  • 4,584
  • 7
  • 34
  • 52
Ali
  • 101
  • 1
  • 2
  • Find and delete or change the existing account. The error is telling you that this person is ALREADY IN Active Directory. – Joel Coel Oct 07 '15 at 19:25
  • no the active Directory was clean when I was trying to run this script. – Ali Oct 07 '15 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 07 '15 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 07 '15 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? – Johan de Haan Oct 12 '15 at 11:03
  • the users are been created but the script is also generating some errors. can you run the script on your own system and see whats happening? – Ali Oct 14 '15 at 19:51
  • "exception calling substring with 2 argument(s) index and length must refer to a location within the string". "New-AdUser : The specified account already exists". – Ali Oct 14 '15 at 19:51

1 Answers1

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"
Nixphoe
  • 4,584
  • 7
  • 34
  • 52
  • 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 07 '15 at 19:53