locked
Add users to AD from CSV RRS feed

  • Question

  • Hi, I have a problem with adding users to AD from my csv file. Can someone help me please? I don´t know where is the mistake. 

    Many Thanks

    CSV:

    SamAccountName;UserPrincipalName;GivenName;Surname;Name;DisplayName;EmailAddress;StreetAddress;City;PostalCode;Country;Description
    Prihlaseni;Prihlaseni@c.local;Radim;Kecisav;Radim Kecisav;Radim Kecisav;midar@tes.cz;U Jarouska 1;Doma;00001;CZ;Toto je uzivatel z CSV
    Prihlaseni1;Prihlaseni1@c.local;Radim1;Kecisav1;Radim1 Kecisav1;Radim1 Kecisav1;midar@tes.cz;U Jarouska 2;Doma;00002;CZ;Toto je uzivatel z CSV1

    Script: 

    Import-Module ActiveDirectory 
    $Users = Import-Csv -Delimiter ";" -Path "C:\temp\newUsers1.csv"  
    foreach ($User in $Users)  
    {  $OU = "OU=Users,DC=c,DC=local"
        New-ADUser -SamAccountName $User.SamAccountName -UserPrincipalName $User.UserPrincipalName -GivenName $User.GivenName -Surname $User.Surname -Name ($User.GivenName + " " + $User.Surname)  -DisplayName ($User.GivenName + " " + $User.Surname) -EmailAddress $User.EmailAddress -StreetAddress $User.StreetAddress -City $User.City -PostalCode $User.PostalCode -Country $User.Country -Description $User.Description -Path $OU
        } 
    

    Error message:

    New-ADUser : Directory object not found
    At line:5 char:5
    +     New-ADUser -SamAccountName $User.SamAccountName -UserPrincipalNam ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (CN=Radim Kecisav,OU=Users,DC=c,DC=local:String) [N 
       ew-ADUser], ADIdentityNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdenti 
       tyNotFoundException,Microsoft.ActiveDirectory.Management.Commands.NewADUser
     
    New-ADUser : Directory object not found
    At line:5 char:5
    +     New-ADUser -SamAccountName $User.SamAccountName -UserPrincipalNam ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (CN=Radim1 Kecis...s,DC=c,DC=local:String) [New-ADU 
       ser], ADIdentityNotFoundException
        + FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdenti 
       tyNotFoundException,Microsoft.ActiveDirectory.Management.Commands.NewADUser
     



    • Edited by Brdisek Thursday, February 13, 2020 8:03 PM
    Thursday, February 13, 2020 7:45 PM

Answers

  • You should not create users in the "User" container.  You need to create a new OU for users as the default "Users" container is for systems level accounts only.  Also it is not possible to apply group Policy to that container.


    \_(ツ)_/

    • Marked as answer by Brdisek Thursday, February 13, 2020 8:39 PM
    Thursday, February 13, 2020 8:04 PM
  • Read the error message. It tells you the exact problem.


    \_(ツ)_/

    • Marked as answer by Brdisek Thursday, February 13, 2020 9:20 PM
    Thursday, February 13, 2020 8:46 PM

All replies

  • Please post your code correctly.

    You need to also post the error message.


    \_(ツ)_/

    Thursday, February 13, 2020 7:49 PM
  • Please post your code according to the link above.


    \_(ツ)_/

    Thursday, February 13, 2020 8:02 PM
  • Sorry for my mistake, I repaired. 
    Thursday, February 13, 2020 8:03 PM
  • You should not create users in the "User" container.  You need to create a new OU for users as the default "Users" container is for systems level accounts only.  Also it is not possible to apply group Policy to that container.


    \_(ツ)_/

    • Marked as answer by Brdisek Thursday, February 13, 2020 8:39 PM
    Thursday, February 13, 2020 8:04 PM
  • Do the following

    Get-AdObject $OU


    \_(ツ)_/

    Thursday, February 13, 2020 8:05 PM
  • Could it be that you're trying to add a new user that's using an email address already in the AD? The 2nd row of your CSV has a duplicate address.

    What error did you receive when you ran your script using that CSV?

     

    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Thursday, February 13, 2020 8:08 PM
  • OK, so I just repair OU to "OU=ouCREDIO" which exists

    And have this message

    New-ADUser : The operation failed because UPN value provided for addition/modification is not uniq
    ue forest-wide
    At line:5 char:5
    +     New-ADUser -SamAccountName $User.SamAccountName -UserPrincipalNam ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (CN=Radim Kecisa...O,DC=c,DC=local:String) [New-ADUse 
       r], ADException
        + FullyQualifiedErrorId : ActiveDirectoryServer:8648,Microsoft.ActiveDirectory.Management.Com 
       mands.NewADUser

    Thursday, February 13, 2020 8:09 PM
  • the email addresses don´t exist, they are just for testing

    When I tried run New-ADUser in command line, it worked. 

    Thursday, February 13, 2020 8:15 PM
  • PS C:\> Import-Module ActiveDirectory 
    $Users = Import-Csv -Delimiter ";" -Path "C:\temp\newUsers1.csv"  
    foreach ($User in $Users)  
    {  $OU = "OU=test,DC=c,DC=local"
        New-ADUser -SamAccountName $User.SamAccountName -UserPrincipalName $User.UserPrincipalName -GivenName $User.GivenName -Surname $User.Surname -Name ($User.GivenName + " " + $User.Surname)  -DisplayName ($User.GivenName + " " + $User.Surname) -EmailAddress $User.EmailAddress -StreetAddress $User.StreetAddress -City $User.City -PostalCode $User.PostalCode -Country $User.Country -Description $User.Description -Path $OU
        } 

    New-ADUser : The operation failed because UPN value provided for addition/modification is not uniq
    ue forest-wide
    At line:5 char:5
    +     New-ADUser -SamAccountName $User.SamAccountName -UserPrincipalNam ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (CN=Radim Kecisav,OU=test,DC=c,DC=local:String) [New- 
       ADUser], ADException
        + FullyQualifiedErrorId : ActiveDirectoryServer:8648,Microsoft.ActiveDirectory.Management.Com 
       mands.NewADUser
     
    New-ADUser : The operation failed because UPN value provided for addition/modification is not uniq
    ue forest-wide
    At line:5 char:5
    +     New-ADUser -SamAccountName $User.SamAccountName -UserPrincipalNam ...
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (CN=Radim1 Kecisav1,OU=test,DC=c,DC=local:String) [Ne 
       w-ADUser], ADException
        + FullyQualifiedErrorId : ActiveDirectoryServer:8648,Microsoft.ActiveDirectory.Management.Com 
       mands.NewADUser
     

    PS C:\> Get-AdObject $OU

    DistinguishedName     Name ObjectClass        ObjectGUID                          
    -----------------     ---- -----------        ----------                          
    OU=test,DC=c,DC=local test organizationalUnit 896c8fb0-1e36-4790-addb-d5188e756c47
    Thursday, February 13, 2020 8:16 PM
  • Read the error message. It tells you the exact problem.


    \_(ツ)_/

    • Marked as answer by Brdisek Thursday, February 13, 2020 9:20 PM
    Thursday, February 13, 2020 8:46 PM
  • If I can suggest something, I found it easier to maintain and play with the code if you use parameters, example below.

    $userparameters = @{
        'SamAccountName' = $User.SamAccountName
        'FirstName' = $User.FirstName
    }

    Thursday, February 13, 2020 8:48 PM