none
How to Create Contacts using Powershell? RRS feed

  • Question

  • Hello,

    In my normal AD machine, I can create Contact using ADUC in any OU.

    By using Quest AD module, I can create Contact using following cmdlet or script -

    Import-Csv $Path | foreach{New-QADObject -ParentContainer "OU=Contacts,DC=first,DC=domain,DC=com" -type "Contact" -name $_.DisplayName -ObjectAttributes @{displayName=$_.DisplayName;mailNickname=$_.Alias;mail=$_.ExternalEmailAddress;proxyAddresses=$_.ExternalEmailAddress}}

    But I need to create CONTACT using Inbuilt Windows PowerShell "Import-Module ActiveDirectory" - and that too without using any Quest AD module.

    Please help me with cmdlet or script through which I can create contact directly (manually entering details in PowerShell) or from CSV.

    Thanks,

    Divyaprakash Koli


    • Edited by Divyaprakash Monday, September 23, 2013 2:51 PM Grammar
    Monday, September 23, 2013 2:50 PM

Answers

  • Hello Tarique,

    Thanks for you time and efforts.

    Finally, the below one is working for me -

    Import-Csv $Path | foreach{New-ADObject -Type Contact -Name $_.DisplayName -OtherAttributes @{'displayName'=$_.DisplayName;'mail'=$_.ExternalEmailAddress;'proxyAddresses'=$_.ExternalEmailAddress;'givenName'=$_.FirstName;'sn'=$_.LastName;'physicalDeliveryOfficeName'=$_.OfficeLocation;'telephoneNumber'=$_.TelephoneNumber;'mobile'=$_.MobileNumber;'facsimileTelephoneNumber'=$_.FaxNumber;'streetAddress'=$_.StreetAddress;'postOfficeBox'=$_.'P.O.Box';'l'=$_.City;'st'=$_.StateorProvince;'postalCode'=$_.PostalCode;'c'='AU';'Title'=$_.JobTitle;'department'=$_.Department;'company'=$_.Company} -Path "OU=$ou1,OU=$ou2,DC=$dc1,DC=$dc2,DC=$dc3"}

    Thanks,

    Divyaprakash Koli

    • Marked as answer by Divyaprakash Monday, September 23, 2013 10:27 PM
    Monday, September 23, 2013 10:27 PM
  • Hello Tarique,

    Thanks for your cmdlet.

    I tried it and contact is being created under main domain.com, but I want to give path or ParentContainer as I used to give under Quest Module, for e.g. If I want to create Contact in OU=Demo.

    Please guide.

    Thanks,

    Divyaprakash Koli

    Hi ,

    You can include  -path parameter  , which specify where you want to create / store your AD contact .

    for example  :

    New-ADObject -Type contact -name Tarique -Path "ou=OuName,dc=domain,dc=com"


    If you found my post helpful, please give it a Helpful vote. If it answered your question, remember to mark it as an Answer. ---------- MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2

    • Marked as answer by Divyaprakash Monday, September 23, 2013 5:01 PM
    Monday, September 23, 2013 4:55 PM

All replies

  • New-ADObject

    http://technet.microsoft.com/en-us/library/ee617260.aspx

    See Example #3


    Santhosh Sivarajan | Houston, TX

    Windows 2012 Book - Migrating from 2008 to Windows Server 2012

    http://www.sivarajan.com/
    FaceBookTwitter LinkedIn SS Tech Forum
    This post is provided ASIS with no warran

    Monday, September 23, 2013 3:04 PM
  • Hi,

    Contact is created by New-Object cmldlet in PowerShell like this :

    new-adobject -Type contact -Name "contactName" -DisplayName "DisplayName "  -Description " Des "  

    For more info :

    http://technet.microsoft.com/en-us/library/ee617260.aspx


    If you found my post helpful, please give it a Helpful vote. If it answered your question, remember to mark it as an Answer. ---------- MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2


    Monday, September 23, 2013 3:09 PM
  • Hello Tarique,

    Thanks for your cmdlet.

    I tried it and contact is being created under main domain.com, but I want to give path or ParentContainer as I used to give under Quest Module, for e.g. If I want to create Contact in OU=Demo.

    Please guide.

    Thanks,

    Divyaprakash Koli

    Monday, September 23, 2013 4:37 PM
  • Hello Tarique,

    Additionally, I don't wish to include "DC=domain,DC=com" as I am directly running script on same server.

    If not then I can put "domain.com" directly.

    Is there any provision for this requirement?

    Thanks,

    Divyaprakash Koli

    Monday, September 23, 2013 4:49 PM
  • Hello Tarique,

    Thanks for your cmdlet.

    I tried it and contact is being created under main domain.com, but I want to give path or ParentContainer as I used to give under Quest Module, for e.g. If I want to create Contact in OU=Demo.

    Please guide.

    Thanks,

    Divyaprakash Koli

    Hi ,

    You can include  -path parameter  , which specify where you want to create / store your AD contact .

    for example  :

    New-ADObject -Type contact -name Tarique -Path "ou=OuName,dc=domain,dc=com"


    If you found my post helpful, please give it a Helpful vote. If it answered your question, remember to mark it as an Answer. ---------- MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2

    • Marked as answer by Divyaprakash Monday, September 23, 2013 5:01 PM
    Monday, September 23, 2013 4:55 PM
  • Hello Tarique,

    Additionally, I don't wish to include "DC=domain,DC=com" as I am directly running script on same server.

    If not then I can put "domain.com" directly.

    Is there any provision for this requirement?

    Thanks,

    Divyaprakash Koli

    No , you can't use  "domain.com" directly . You have to use the complete path  as Distinguished Name format .

    one thing you can do , store  the path in a Variable like this :

    $path="ou=Myou,dc=domain,dc=com" 

    then  use like this :

    New-ADObject -Type contact -name Tarique -Path $path


    If you found my post helpful, please give it a Helpful vote. If it answered your question, remember to mark it as an Answer. ---------- MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2


    Monday, September 23, 2013 4:59 PM
  • Hello Tarique,

    Thanks for you time and efforts.

    Finally, the below one is working for me -

    Import-Csv $Path | foreach{New-ADObject -Type Contact -Name $_.DisplayName -OtherAttributes @{'displayName'=$_.DisplayName;'mail'=$_.ExternalEmailAddress;'proxyAddresses'=$_.ExternalEmailAddress;'givenName'=$_.FirstName;'sn'=$_.LastName;'physicalDeliveryOfficeName'=$_.OfficeLocation;'telephoneNumber'=$_.TelephoneNumber;'mobile'=$_.MobileNumber;'facsimileTelephoneNumber'=$_.FaxNumber;'streetAddress'=$_.StreetAddress;'postOfficeBox'=$_.'P.O.Box';'l'=$_.City;'st'=$_.StateorProvince;'postalCode'=$_.PostalCode;'c'='AU';'Title'=$_.JobTitle;'department'=$_.Department;'company'=$_.Company} -Path "OU=$ou1,OU=$ou2,DC=$dc1,DC=$dc2,DC=$dc3"}

    Thanks,

    Divyaprakash Koli

    • Marked as answer by Divyaprakash Monday, September 23, 2013 10:27 PM
    Monday, September 23, 2013 10:27 PM
  • I am glad to help you.



    If you found my post helpful, please give it a Helpful vote. If it answered your question, remember to mark it as an Answer. ---------- MCITP - Exchange 2010 | MCITP - Windows Server 2008 R2

    Tuesday, September 24, 2013 3:40 AM
  • Hello,

    Is it possible to create this contact without creating a @domain email address? I only want an external address for my contacts. At this point I have to remove the @domain email address after the contact is created. Thank you

    Thursday, July 10, 2014 3:54 PM
  • Yes, if using the ActiveDirectory module to create a contact,  a 'mail' attribute is not mandatory.  The only attribute you must specify a value for is 'name'.

    New-ADObject -Type contact -Name "Test Contact"
    If using the Exchange module to create contacts, I believe email addresses are required, IIRC.


    Friday, August 8, 2014 6:11 PM
  • Here is my little addition to all of these great examples:

    Import-Csv $path | ForEach {New-MailContact -Name $_.FirstName -Alias $_.Alias -ExternalEmailAddress $_.ExternalAddress -OrganizationalUnit "OU=xxxxxxxxxxx,OU=xxxxxxxxx,DC=xx,DC=xx,DC=xx" | Set-MailContact -HiddenFromAddressListsEnabled $true}

    In the case of my company, we, for security meassures, hide every external contact, so there you have how to, after creating them all.

    Cheers!

    Wednesday, June 17, 2015 7:49 PM
  • how can i update "Manager" property of a contact from an excel sheet. Everything else looks good. Please if you can help.
    Sunday, December 11, 2016 2:30 PM
  • First, you have asked in an old answered thread. Either this question should be split, or you should ask in a new thread. Second, this thread does not discuss updating contacts from a spreadsheet. And you probably need to post enough code to show how you are attempting to update contacts.

    You need to use Set-ADObject to update contacts, which requires that objects be identified by distinguished name. It also does not have a Manager parameter, so you must use the -Add (or -Replace) parameter, and you must specify the distinguished name of the manager.

    Assuming your spreadsheet has the DN of the contacts in column 1 and the DN of the managers in column 2, and also that the first row is headers that should be skipped, the following should get you started:

    $ExcelPath = "c:\scripts\Contacts.xls"
    
    $Excel = New-Object -ComObject "Excel.Application"
    $Workbooks = $Excel.Workbooks
    $Workbook = $Workbooks.Open($ExcelPath)
    $Worksheets = $Workbook.Worksheets
    $Sheet = $Worksheets.Item(1)
    $Range = $Sheet.UsedRange
    $Rows = $Range.Rows
    
    For ($k = 2; $k -le $Rows.Count; $k = $k + 1)
    {
        # Contact DN in column 1.
        $DN = $Sheet.Cells.Item($k, 1).Text
        # Manager DN in column 2
        $Manager = $Sheet.Cells.Item($k, 2).Text
        Set-ADObject -Identity $DN -Add @{manager=$Manager}
    }
    

    However, the above has not been tested.


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Sunday, December 11, 2016 7:29 PM
  • Hi Divyaprakash,

    Where did you set the variable for Path $ou1/ou2 etc.

    When I try to set the path as -Path "OU=Contacts,DC=Domain,DC=Domain1" to specific folder path on AD, it throws me errors. 

    Thank you

    Anu

    Thursday, August 1, 2019 9:04 PM