Share via


Microsoft Exchange Server 2010: Get to Know the Exchange Management Shell

The Exchange Management Shell gives you complete control over all aspects of your Exchange infrastructure, powered by Windows PowerShell.

Excerpted from “Exchange 2010 - A Practical Approach,” published by Red Gate Books (2009).

Jaap Wesselius

The Exchange Management Shell (EMS) is a complete management interface with which you can manage all aspects of an Exchange organization. The EMS is the primary management interface. The Exchange Management Console (EMC) is actually built “on top” of it. Every action you take in the EMC is translated on the fly into an EMS command and executed.

Under the hood, the EMS uses Windows PowerShell 2.0. Combined with the remote management capabilities in Windows Server 2008 and Windows Server 2008 R2, you can remotely manage your Exchange environment. The EMS is actually a superset of commands built on top of Windows PowerShell.

In addition to the command-line interface, Windows PowerShell 2.0 also has an Integrated Scripting Environment, which is an integrated GUI. You can use this to easily create Windows PowerShell scripts. It’s also integrated with Windows Remote Management, so you can use Windows PowerShell to remotely manage your Exchange 2010 environment using the standard HTTPS protocol. All you need is a workstation or a server running Windows PowerShell 2.0.

Fire Up the Shell

When the EMS is started, you’ll basically see an empty box with just a command prompt—exactly like the Windows command prompt. You can get a list of available commands at this stage by entering Get-Command.

For the benefit of those die-hard GUI administrators, a Windows PowerShell command consists of two parts: a noun and a verb. Verbs can be instructions like get, set, new, remove, enable, disable and so on. The Noun component can be any object in Exchange Server. Just combine the noun and the verb like this:

  • Get-ExchangeServer: Retrieve a list of all Exchange 2010 Servers in the organization.
  • Set-MailboxDatabase: Set a property on a Mailbox Database.
  • New-Mailbox:  Create a new mailbox-enabled User.
  • Remove-Mailbox:  Delete a user object and its mailbox.

If you’re new to Windows PowerShell and want to learn more about the commands, a quick Web search will turn up scores of learning resources. You could also start following the Windows PowerShell columns on TechNet.

EMS Help

If there’s anything about which you’re unsure when using the EMS, check the Quick Reference Guide. This is located in C:\Program Files\Microsoft\ExchangeServer\v14\bin\en\ExQuick.htm. This contains the most important and most-used cmdlets and their variables.

If you need help on the fly, you can use the built-in help function in the EMS. To get a list of all available help items, just type “help *”. If you need help about a specific cmdlet, just type help and the name of the cmdlet. To get help about mail-enabling an existing user, for example, just type “help enable-mailbox”.

Pipelining

Another great feature in Windows PowerShell and the EMS is pipelining. This uses the output of one cmdlet as the input for a second. This can drastically reduce the amount of work you need to put in to accomplish relatively complex tasks. It’s limited only by your ingenuity.

For example, if you want to move all mailboxes in a mailbox database called “Mailbox Database 1988197524” to another mailbox database called “Mailbox Database 0823751426,” use the following command:

Get-Mailbox –Database "Mailbox Database 1988197524" | New-MoveRequest –TargetDatabase "Mailbox Database 0823751426"

This is what happens:

Get-Mailbox –Database “Mailbox Database 1988197524” retrieves a list of all mailboxes in this particular database. The output of this cmdlet is used as the input of the second cmdlet—the online request to move mailboxes to the other database. You can also use more specific queries. For example, to get a list of all mailboxes whose name starts with “Chris,” you would use the following command:

Get-Mailbox | where-object {$_.name –like "Chris*"}

You can then use this as the input for a request to move all these mailboxes to another database:

Get-Mailbox | where-object {$_.name –like "Chris*"} | New-MoveRequest ' –TargetDatabase "Mailbox Database 0823751426"

Creating Bulk Users

This can be very useful, particularly when you need to create a lot of mailboxes in a hurry. Suppose you have an organizational unit (OU) named “Sales” in Active Directory, which contains 100 user objects. This command will create a mailbox for each user in this OU:

Get-User –OrganizationalUnit "Sales" | Enable-Mailbox –Database "Mailbox Database 0823751426"

When there are multiple OUs called “Sales,” you have to specify the complete path of the OU you want to use:

Get-User –OrganizationalUnit "E14.local/Account/Sales" | Enable-Mailbox –Database "Mailbox Database 0823751426"

You can also filter the output of the Get-User command with the –Filter parameter. For example, to Mailbox-Enable all users whose company attribute is set to “Inframan,” enter the following command:

Get-User –Filter {(Company –eq "Inframan")} | Enable-Mailbox –Database "Mailbox Database 0823751426"

If you want to be even more specific—for example, to Mailbox-Enable all users whose company attribute is set to “Inframan” and whose department attribute is set to “Intern”—enter the following command:

Get-User –Filter {(Company –eq "Inframan") -AND (Department –eq "Intern")} | Enable-Mailbox –Database "Mailbox Database 0823751426"

The following operations are available for the –Filter option:

  • -and
  • -or
  • -not
  • -eq (equals)
  • -ne (does not equal)
  • -lt (less than)
  • -gt (greater than)
  • -like (string comparison)
  • -notlike (string comparison)

In some cases, you’ll find it useful to import a list of users from a CSV file. You can import these types of lists from another Active Directory or even a human resources application. It’s relatively easy to import a CSV file using Windows PowerShell. The only thing you need to be mindful of is that the –Password option doesn’t accept clear text input. You have to convert the input to this field to a secure string:

$Database="Mailbox Database 1563944384" $UPN="e2010.local" $users = import-csv $args[0] function SecurePassword([string]$password) { $secure = new-object System.Security.SecureString $password.ToCharArray() | % { $secure.AppendChar($_) } return $secure }foreach ($i in $users) { $sp = SecurePassword $i.password $upn = $i.FirstName + "@"+ $upn $display = $i.FirstName + " "+ $i.LastName New-Mailbox -Password $sp -Database $Database -UserPrincipalName $UPN -Name $i.FirstName -FirstName $i.FirstName -LastName $i.LastName -OrganizationalUnit $i.OU }

On the first three lines, there are three parameters set. These parameters are used during the actual creation of the user and the mailbox. The file is read in a ForEach loop, and the actual users and the mailboxes are created as the loop progresses.

The SecurePassword function reads the password from the output CSV file and converts it to a secure string—which is used, in turn, as the password input during the creation of the users. The CSV file itself is formatted like this:

FirstName, LastName, Password, OU Jaap, Wesselius, Pass1word, Accounts Michael, Francis, Pass1word, Accounts Michael, Smith, Pass1word, Accounts John, Doe, Pass1word, Accounts

To make this script usable, save the script file as “create.ps1” in a directory like c:\scripts. You’ll also need to save the CSV output file as users.csv in the same directory. To actually use the script, open a Windows PowerShell command prompt, navigate to the c:\scripts directory and enter the following command:

.\create.ps1 users.csv

These techniques will help you get the best use out of the EMS, and use some of the power of Windows PowerShell to manage your Exchange infrastructure.

Jaap Wesselius

Jaap Wesselius is the founder of DM Consultants, a company with a strong focus on messaging and collaboration solutions. After working at Microsoft for eight years, Wesselius decided to commit more of his time to the Exchange community in the Netherlands, resulting in an Exchange Server MVP award in 2007. He is also a regular contributor at the Dutch Unified Communications User Group and a regular author for Simple-Talk.

Learn more about “Exchange 2010 - A Practical Approach” at red-gate.com/our-company/about/book-store.