Solved

CREATE MAIL CONTACT FROM EXCHANGE POWERSHELL THROUGH SCRIPT

Posted on 2014-12-28
12
113 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
  • 6
  • 5
12 Comments
 
LVL 69

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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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 69

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 69

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 69

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 69

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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

This article explains how to install and use the NTBackup utility that comes with Windows Server.
The new Gmail Phishing Scam going around is surprising even the savviest of users with its sophisticated techniques.
This video demonstrates how to sync Microsoft Exchange Public Folders with smartphones using CodeTwo Exchange Sync and Exchange ActiveSync. To learn more about CodeTwo Exchange Sync and download the free trial, go to: http://www.codetwo.com/excha…
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…

773 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