Exchange 2007 was one of the first Powershell really integrated applications. Every configuration item can be set with the Exchange Management Shell, often the usage of the Exchange Management Shell is the only way.
In the next couple of blogs the most used Exchange Powershell commands will be explained. In this blog I will look at the Contact, Mail User and Mailbox related commands and their most used parameters. Feel free to supply me with feedback.
When working in projects or when you maintain an Exchange 2010 environment you want to automate as much as possible. With the following commands you can automate the creation of Mail Users, Mailboxes and Contacts.
Difference between objects
Mail users and Mail Contacts have external e-mail addresses and both contain information about people outside your Exchange Server 2010 organization that can be displayed in the global address list (GAL) and other address lists. However, unlike a mail contact, a mail user has Active Directory logon credentials and can access resources. The Exchange mailbox object reprisents a mailbox in the Exchange Server 2010 organization.
The Commands
Command | Explanation |
new-mailbox (create a new mailbox) | |
-Name | Name of the owner of the mailbox |
-Alias | Exchange Alias |
–Organizationalunit | Active Directory OU |
–Displayname | The DisplayName is the name displayed in Microsoft Outlook for the mail user |
–Lastname | The lastname of an user |
–Firstname | The firstname of an user |
–ResetPasswordOnNextLogon | Force the user to reset the password |
-Database | The database where the mailbox is created |
-Password | The initial password. |
Example | $password = Read-Host “Enter password” –AsSecureStringNew-Mailbox –UserPrincipalName jdoe@msexchangeblog.local -Alias jdoe -Database “Mailbox Database 1” -Name JDoe –OrganizationalUnit “msexchangeblog.local/Users” -Password $password –FirstName John –LastName Doe –DisplayName “Doe, John” –ResetPasswordOnNextLogon $True |
set-mailbox(set properties of the mailbox) | |
-Identity | Identity of the object |
–Displayname | The DisplayName is the name displayed in Microsoft Outlook for the mailbox. |
–Lastname | The lastname of an user |
–Firstname | The firstname of an user |
–ExternalEmailAddress | External Emailaddress of the user |
–HiddenFromAddressListsEnabled | Hide the contact from the Global Address List of Exchange 2010. |
–EmailAddressPolicyEnabled | Disallow email address policy updates |
Example | |
Get-mailbox (get the information of the mailbox) | |
-Identity | Identity of the object |
Example | get-mailbox -identity “Doe, John” |FL |
Disable-mailbox (disable a mailbox, the Exchange properties are removed from the userobject) | |
-Identity | Identity of the object |
Example | disable-mailbox -identity “Doe, John” |
Remove-mailbox (remove a mailbox) | |
-identity | Identity of the object |
Example | remove-mailbox -identity “Doe, John” |
new-mailcontact (create a new mail contact) | |
-Name | Name of the Contact |
–ExternalEmailAddress | External Emailaddress of the user |
-Alias | Exchange Alias |
-Organizationalunit | Active Directory OU |
–Displayname | The DisplayName is the name displayed in Microsoft Outlook for the mail user. |
–Lastname | The lastname of an user |
–Firstname | The firstname of an user |
Example | new-mailcontact –name “Doe, John” –externalemailaddress “john.doe@msexchangeblog.nl” –alias “JDoe” –displayname “Doe, John” –lastname “Doe” –firstname “John” –OrganizationUnit “msexchangeblog.nl/contacts” |
set-mailcontact(set properties of the mail contact) | |
-Identity | Identity of the object |
–Displayname | The DisplayName is the name displayed in Microsoft Outlook for the mail user. |
–Lastname | The lastname of an user |
–Firstname | The firstname of an user |
–ExternalEmailAddress | External Emailaddress of the user |
–HiddenFromAddressListsEnabled | Hide the contact from the Global Address List of Exchange 2010. |
–EmailAddressPolicyEnabled | Disallow email address policy updates |
Example | |
Get-Mailcontact (get the information of the mail contact) | |
-Identity | Identity of the object |
Example | get-mailcontact -identity “Doe, John” |FL |
Disable-Mailcontact (disable a mail contact) | |
-Identity | Identity of the object |
Example | disable-mailcontact -identity “Doe, John” |
Remove-Mailcontact (remove a mail contact) | |
-Identity | Identity of the object |
Example | remove-mailcontact -identity “Doe, John” |
new-mailuser(create a new mailuser) | |
-Name | Name of the Contact |
–ExternalEmailAddress | External Emailaddress of the user |
-Alias | Exchange Alias |
-Password | The password of the mailuser |
-Displayname | The DisplayName is the name displayed in Microsoft Outlook for the mail contact. |
–Lastname | The lastname of an user |
–Firstname | The firstname of an user |
-Name | The fullname of the Mail User |
–Organizationalunit | Active Directory OU |
–UserPrincipalName | |
Example | $password = Read-Host “Enter password” –AsSecureStringnew-mailuser –name “Doe, John” –externalemailaddress “john.doe@msexchangeblog.nl” –alias “JDoe” –displayname “Doe, John” –lastname “Doe” –firstname “John” –OrganizationalUnit “msexchangeblog.nl/mailusers” -password:$password –userprincipalname “jdoe@msexchangeblog.local” |
set-mailuser (set properties of the mail user) | |
-Identity | Identity of the object |
–Displayname | The DisplayName is the name displayed in Microsoft Outlook for the mail contact. |
–Lastname | The lastname of an user |
–Firstname | The firstname of an user |
–ExternalEmailAddress | External Emailaddress of the user |
–HiddenFromAddressListsEnabled | Hide the contact from the Global Address List of Exchange 2010. |
–EmailAddressPolicyEnabled | Disallow email address policy updates |
Example | |
Enable-Mailuser (enable a existing user as mailuser) | |
-Identity | Identity of the object |
–ExternalEmailAddress | External Emailaddress of the user |
Example | enable-mailuser -identity “Doe, John” -ExternalEmailAddress “john.doe@msexchangeblog.nl” |
Get-Mailuser (get the information of the mail user) | |
-Identity | Identity of the object |
Example | get-mailuser -identity “Doe, John” |FL |
Disable-Mailuser (disable a mail user, the Exchange properties are removed from the userobject) | |
-Identity | Identity of the object |
Example | disable-mailuser -identity “Doe, John” |
Remove-Mailuser (remove a mail user and the userobject) | |
-Identity | Identity of the object |
Example | remove-mailuser -identity “Doe, John” |
Automation
Creating new mailboxes with an CSV file as an input file
When using a CSV file as an input for creating a mailbox you must create a file with the following information: name,displayname,lastname,firstname,password,ou,alias,database,resetpasswordonnextlogon
import-csv input.csv | ForEach { new-mailbox -name $_.name –displayname $_.displayname –lastname $_.lastname –firstname $_.firstname -password $_.password –organizationalunit $_.ou -alias $_.alias, -database $_.database, –resetpasswordonnextlogon $_.resetpasswordonnextlogon}
Creating new mail contacts with an CSV file as an input file
When using a CSV file as an input for creating a contacts you must create a file with the following information: displayname,lastname,firstname,emailaddress,ou,alias
import-csv input.csv | ForEach { new-mailcontact -name $_.displayname –lastname $_.lastname –firstname $_.firstname –externalemailaddress $_.emailaddress –organizationalunit $_.ou -alias $_.alias }
Creating new mail users with an CSV file as an input file
When using a CSV file as an input for creating a contacts you must create a file with the following information: name,displayname,lastname,firstname,password,emailaddress,ou,alias
import-csv input.csv | ForEach { new-mailuser -name $_.name –displayname $_.displayname –lastname $_.lastname –firstname $_.firstname -password $_.password –externalemailaddress $_.emailaddress, –organizationalunit $_.ou -alias $_.alias }
In the next blog will the different kind of distribution lists will be the topic.
I have below csv fields, i am not able to import phone numebr in excahnge 2010
displayname,lastname,firstname,emailaddress,phone,alias,Department
her is my cmdlet:
import-csv sample1.csv | ForEach { new-mailcontact -name $_.displayname -lastname $_.lastname -firstname $_.firstname -externalemailaddress $_.emailaddress -organizationalunit “Contacts” -Phone $_.phone -alias $_.alias -Department $_.Department }
error:
A positional parameter cannot be found that accepts argument ‘-Phone’.
+ CategoryInfo : InvalidArgument: (:) [New-MailContact], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,New-MailContact
I am trying to hide a group of newly created external contacts from the GAL. I am stuck and can not figure out the correct combination of .csv headers and command lines to accomplish this.
Ideally I would like to hide them based on their email address. I have a .csv with a list of email addresses and a header of ExternalEmailAddress. I am then running thge following command in PS…
Import-CSV “c:scriptfileshide external.csv” | ForEach{Set-MailContact -ExternalEmailAddress $_.ExternalEmailAddress -HiddenFromAddressListsEnabled $true”}
Can you please let me know what I am doing wrong. I am new to Exchange and PS and trying to fight my way through. Any help is much appreciated