Solved

CREATE MAIL CONTACT FROM EXCHANGE POWERSHELL THROUGH SCRIPT

Posted on 2014-12-28
12
175 Views
Last Modified: 2015-03-19
Dear Experts,

I want to create multiple  mail contacts through powershell.
Can i have script and example of CSV file.
0
Comment
Question by:msretailit
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
12 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 40520311
That is easy - to some extent.  You need to tell what properties you want to set for a contact, that determines how the CSV file should look like. Basically, the code is:
Import-CSV c:\temp\mailcontacts.csv | % {
  New-MailContact -name $_.Name -ExternalEmailAddress $_.EMail
}

Open in new window

The CSV file:
Name,EMail,OtherStuff
Me,me@mydomain.com,Not used

Open in new window

OtherStuff is not used here, just showing that you can have additional columns.
0
 

Author Comment

by:msretailit
ID: 40520373
Got this error

Import-Csv : Cannot process argument because the value of argument "name" is invalid. Change the value of the "name" ar
gument and run the operation again.
At line:1 char:11
+ Import-CSV <<<<  c:\temp\mailcontacts.csv | % {
    + CategoryInfo          : InvalidArgument: (:) [Import-Csv], PSArgumentException
    + FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.ImportCsvCommand
0
 
LVL 19

Expert Comment

by:R--R
ID: 40520386
You have to use the command in a single line.

You can also check this if it works.

Create an input file name contacts.csv with the following attributes and values
 
name,firstname,lastname,externalemailaddress

Open the Exchange management shell and run the below cmd after copying all contacts to the path of shell:

Import-Csv “contacts.csv” | ForEach {New-MailContact -Name $_.Name -Firstname $_.FirstName -LastName $_.LastName -ExternalEmailAddress $_.ExternalEmailAddress -OrganizationalUnit “Domainname/OUname”}

Reffered from http://msexchangeguru.com/2012/11/16/contacts-in-bulk/
(Prabhat Nigam)
0
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 

Author Comment

by:msretailit
ID: 40520399
OK i got below command working

" Import-Csv C:\ExternalContacts.csv|%{New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName}"

and csv file saved was externalemailaddress, name, firstname,lastname.

I got add all the contacts by above command.

But i want to remove tickmark of automatic update email address policy from contacts and also i want to add email address.
Anyone have idea.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40520417
Note that the shown command is not different from what I posted. I just left out some fields, as I don't know what you need. The error shown (usually) results from the Name not being in the expected format to automatically determine FirstName and LastName from it.
Whatsoever, the CSV is settled now. But I don't get what you mean by "add email address" - this is exactly what you do. Or do you need to add more than one address?
The other requirement can be achieved if we set PrimarySmtpAddress, because that will disable the email policy setting too:
Import-Csv C:\ExternalContacts.csv| % {
  New-MailContact -Name $_.Name -DisplayName $_.Name -PrimarySmtpAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName
}

Open in new window

0
 

Author Comment

by:msretailit
ID: 40520433
Hello Olemo,

I have already made contacts with above command but now it make external address as primary addreses.

Now i want to disable email policy setting from all that contacts and want to add one more addresse
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40520489
As said, setting PrimarySmtpAddress disables the email policy setting, so no further action required.

What do you gain from adding email addresses? Remember: an Exchange Contact is not the same as a Outlook Contact. I'm not certain that you can select a secondary email address when sending email; however, it will map incoming mails against the contact info for all email addresses.

Whatsover, if you insist, add a column "OtherAddresses" to your CSV file, having comma separated addresses enclosed in double quotes.
externalemailaddress, name, firstname,lastname, otherAddresses
me@mydomain.com, It is Me, It, Me, "smtp:me@otherdomain.com, smtp:me@goggle.com"

Open in new window

Then use
Import-Csv C:\ExternalContacts.csv| % {
  New-MailContact -Name $_.Name -DisplayName $_.Name -PrimarySmtpAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName |
    Set-MailContact -EmailAddresses ("smtp:"+$_.ExternalEmailAddress; $_.OtherAddresses -split ',') -EmailAddressPolicyEnabled $false
}

Open in new window

Please see http://technet.microsoft.com/en-us/library/aa995950(v=exchg.141).aspx and http://technet.microsoft.com/en-us/library/bb124519(v=exchg.141).aspx for details about what you can set with which cmdlet.
0
 

Author Comment

by:msretailit
ID: 40667760
Dear Team,

I got below error


[PS] C:\Windows\system32>Import-Csv C:\TEMP\EmailCreation.csv| % {New-MailContact -Name $_.Name -DisplayName $_.Name -Pr
imarySmtpAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName | Set-MailContact -EmailAddresses
 ("smtp:"+$_.ExternalEmailAddress; $_.OtherAddresses -split ',') -EmailAddressPolicyEnabled $false}
Missing closing ')' in expression.
At line:1 char:249
+ Import-Csv C:\TEMP\EmailCreation.csv| % {New-MailContact -Name $_.Name -DisplayName $_.Name -PrimarySmtpAddress $_.Ex
ternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName | Set-MailContact -EmailAddresses ("smtp:"+$_.External
EmailAddress <<<< ; $_.OtherAddresses -split ',') -EmailAddressPolicyEnabled $false}
    + CategoryInfo          : ParserError: (CloseParenToken:TokenId) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingEndParenthesisInExpression
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 40668267
I'm not amused having to deal with this after 3 months without any response ...
Import-Csv C:\ExternalContacts.csv| % {
  New-MailContact -Name $_.Name -DisplayName $_.Name -PrimarySmtpAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName |
    Set-MailContact -EmailAddresses $("smtp:"+$_.ExternalEmailAddress; $_.OtherAddresses -split ',') -EmailAddressPolicyEnabled $false
}

Open in new window

0
 

Author Comment

by:msretailit
ID: 40669804
Hey Olemo,

Sorry for delay due to some issues.

I got below error again


[PS] C:\Windows\system32>Import-Csv C:\TEMP\EmailCreation.csv| % { New-MailContact -Name $_.Name -DisplayName $_.Name -P
rimarySmtpAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName | Set-MailContact -EmailAddresse
s $("smtp:"+$_.ExternalEmailAddress; $_.OtherAddresses -split ',') -EmailAddressPolicyEnabled $false }

cmdlet New-MailContact at command pipeline position 1
Supply values for the following parameters:
ExternalEmailAddress:
Cannot process argument transformation on parameter 'ExternalEmailAddress'. Cannot convert value "" to type "Microsoft.
Exchange.Data.ProxyAddress". Error: "The e-mail address cannot be empty."
    + CategoryInfo          : InvalidData: (:) [New-MailContact], ParameterBindin...mationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,New-MailContact
0
 

Author Comment

by:msretailit
ID: 40669805
This is csv file


ExternalEmailAddress, name, firstname,lastname, otherAddresses
1@gmail.com,M D,M,D,1@Officedomain.com
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40673374
Obviously we have to set both the ExternalEmailAddress and PrimarySmtpAddress here.
Import-Csv C:\ExternalContacts.csv| % {
  New-MailContact -Name $_.Name -DisplayName $_.Name -ExternalEmailAddress $_.ExternalEmailAddress `
        -PrimarySmtpAddress $_.ExternalEmailAddress -FirstName $_.FirstName -LastName $_.LastName |
    Set-MailContact -EmailAddresses $("smtp:"+$_.ExternalEmailAddress; $_.OtherAddresses -split ',') -EmailAddressPolicyEnabled $false
}

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
After hours on line I found a solution which pointed to the inherited Active Directory permissions . You have to give/allow permissions to the "Exchange trusted subsystem" for the user in the Active Directory...
In this video we show how to create an email address policy in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Mail Flow…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Suggested Courses
Course of the Month9 days, 13 hours left to enroll

623 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question