Import Contact AD

This topic contains 17 replies, has 3 voices, and was last updated by  Florian Schinas 1 year, 8 months ago.

  • Author
  • #45253

    Florian Schinas

    Hello everyone,

    I want to import contacts that I have received a CSV file in our AD.
    So I made a script and edit the CSV file that any corersponde.

    Here is the script:

    Import-Module ActiveDirectory 
    $Users = Import-Csv -Path "D:\userslist.csv"  
    foreach ($User in $Users)  
        $OU = "OU=TestGPO,DC=coditel,DC=be"  
        $FirstName = $User.givenName 
        $LastName = $
        $DisplayName = $LastName + " " +$FirstName
        $Company = $
        $Department = $User.department
        $EMail = $User.mail
        $TelephoneNumber = $User.telephoneNumber
        $Mobile = $
        New-ADObject -type Contact -Path $OU -Name $DisplayName -DisplayName $DisplayName -GivenName $user.givenName -OtherAttributes @{'sn'=$LastName; 'company'=$company; 'department'=$departement; 'mail'=$EMail; 'telephoneNumber'=$telephoneNumber; 'mobile'=$mobile}

    Looks like this Excel file:

    sn givenName mail Company department telephoneNumber mobile
    PANTA Sandra R SERVICE CLIENT SA RD Aix – Groupe 7 +33 4 00 00 00 00 +33 6 00 00 02 00

    I started the script from my PC (not the AD because I have not Excel installed on it) and he created me a user / but nothing else!

    Should I start the script from the server? Should I install excel on the server? Script wrong?

    Thank you!

  • #45257

    Mark Prior

    Are you not better off creating this through exchange if it is a contact ? something like the below

    you do not need excel to read the csv file

     $Contacts = "D:\userslist.csv"
     Import-Csv $contacts | ForEach-Object { New-MailContact -Name $_."Name" -ExternalEmailAddress $_."Email-Address" -FirstName $_."firstname" -LastName $_."lastname" -OrganizationalUnit "OU=Contacts,OU=Users,DC=Domain,DC=Name,DC=com" -Alias $_."Alias" }

    csv just needs the appropriate headers in the top row

    Name, Email-Address, Firstname etc.........

    • This reply was modified 1 year, 8 months ago by  Mark Prior.
    • This reply was modified 1 year, 8 months ago by  Mark Prior.
  • #45261

    Florian Schinas

    That change if I put them in the Exchange?
    Just execute the script from the PowerShell console of the Exchange then?

    I know add fields in the Exchange department, company, phone number, etc.? In your example not so I ask myself ...

    The Excel should look like then? :

    Lastname Firstname Email-Address Company(??) Department(??) telephoneNumber(??) mobile(??)
    PANTA Sandra R SERVICE CLIENT SA RD Aix – Groupe 7 +33 4 00 00 00 00 +33 6 00 00 02 00

    Thanks for help in advance!

    PS : Alias is use for what ? I dont need a alias .. I dont have that column in my CSV ..

    • #45265

      Mark Prior

      from memory i do not think the new-mailcontact supports the telephone etc.., try adding using the set command at the end

       $Contacts = "D:\userslist.csv"
       Import-Csv $contacts | ForEach-Object { New-MailContact -Name $_."Name" -ExternalEmailAddress $_."Email-Address" -FirstName $_."firstname" -LastName $_."lastname" -OrganizationalUnit "OU=Contacts,OU=Users,DC=Domain,DC=Name,DC=com" -Alias $_."Alias" | Set-Contact -MobilePhone $_."mobile" -Department $_."Department" -Phone $_."phone" -Company $_."company"}

      not tested as i do not have laptop with me today, the

    • #45267

      Florian Schinas

      I'll try that in a moment, but suddenly, you may realize why I wanted to go through the AD rather than by the Exchange?

  • #45277

    Florian Schinas
  • #45283

    Mark Prior

    looking at the error you havnt changed OU to suite your environtment,

    error looks to start at charector 71 "Externalmailaddress" have you updated CSV to read correctly / have correct column headings?

    i.e added alias & renamed values?

    if alias is not needed just remove

    -Alias $_."Alias"

    from the line

  • #45287

    Curtis Smith

    Hi Florian,
    There are a couple of code issues, but what do you mean "he created me a user / but nothing else!". When you use New-ADObject to create a contact, it just creates an AD Contact. If your AD Object is in fact being created, then you look at the Object class Attribute, it should be "Contact". This does not; however, mean you will be able to see it as a contact in an Exchange environment. To do that, you must enable it as a MailContact. The easiest way to do that is to use the Enable-MailContact Exchange cmdlet.

    Script issues:
    1) You have an extra "e" in the department variable on the new-adobject cmdlet. IE $departement should be $department.
    2) GivenName is not a valid Parameter for Get-ADObject. You will need to set this value in the OtherAttributes hashtable.

    • #45322

      Florian Schinas

      Hi Curtis,

      I change what you tell me but .. No success ..
      How i can execute this script ? Copy/Past on a powershell console doesn't work ?

      Edit :
      Error when i start on AD :
      New-ADObject : Cannot bind parameter 'OtherAttributes' to the target. Exception
      setting "OtherAttributes": "Object reference not set to an instance of an obje
      At D:\Extract2.ps1:14 char:103
      + New-ADObject -type Contact -Path $OU -Name $DisplayName -DisplayName $Dis
      playName -OtherAttributes <<<< @{'sn'=$LastName; 'givenName'=$FirstName; 'comp any'=$company; 'department'=$department; 'mail'=$EMail; 'telephoneNumber'=$tele phoneNumber; 'mobile'=$mobile} + CategoryInfo : WriteError: (:) [New-ADObject], ParameterBinding Exception + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory .Management.Commands.NewADObject Ty !

  • #45289

    Florian Schinas

    Really sorry, a bit rotten morning I did not concentrate, I did not blunder!
    I will withdraw Alias. email Level I put CECEI for the column: Email-Address

  • #45320

    Florian Schinas


    When i run the script from the Exchange :

    Cannot bind argument to parameter 'Name' because it is an empty string.
    + CategoryInfo : InvalidData: (:) [New-MailContact], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,New-MailContact

    Script :

     $Contacts = "\\coditel-12\Engineering\6-IT\userslist.csv"
     Import-Csv $contacts | ForEach-Object { New-MailContact -Name $_."Name" -ExternalEmailAddress $_."Email-Address" -FirstName $_."firstname" -LastName $_."lastname" -OrganizationalUnit "OU=TestGPO,DC=Domain,DC=Coditel,DC=be" | Set-Contact -MobilePhone $_."mobile" -Department $_."Department" -Phone $_."phone" -Company $_."company"}

    Excel :

    Name lastname firstname Email-Address company Department phone mobile
    PANTA Sandra PANTA Sandra SERVICE CLIENT SA RD Aix – Groupe 7 +33 4 00 00 90 00 +33 6 00 00 02 78

    Someone can help me ? Thank you!

  • #45325

    Mark Prior

    Hi Florian

    the script works fine, you may want to check csv file, heres a pic of the script the file and the produced contact


  • #45329

    Florian Schinas

    Hi Mark,

    Here the script :

     $Contacts = "\\coditel-12\Engineering\6-IT\userslist.csv"
     Import-Csv $contacts | ForEach-Object { New-MailContact -Name $_."Name" -ExternalEmailAddress $_."Email-Address" -FirstName $_."firstname" -LastName $_."lastname" -OrganizationalUnit "OU=TestGPO,DC=Domain,DC=Coditel,DC=be" | Set-Contact -MobilePhone $_."mobile" -Department $_."Department" -Phone $_."phone" -Company $_."company"}

    Here the file excel :

    I dont know where i'm bad ...

    Here my test :

  • #45340

    Florian Schinas
  • #45347

    Mark Prior

    sorry florian i cant see whats wrong with that, from pics shown this should be working, i can see the name column is there and populated

  • #45349

    Florian Schinas

    The name is a fusion of two column on my excel, but i convert it on csv, so i don't think the problem can from my Excel ?
    I really want import this user soon, i don't know why don't work, is really strange !

    And why my command don't show the same that you for user ??

  • #45433

    Florian Schinas

    Ok I found the problem.
    My Excel is semicolon delimited.
    I just added -Delimiter ";" and it works!

You must be logged in to reply to this topic.