WinDeveloper O365 Mailer FREE for 1 Year

WinDeveloper IMF Tune
WinDeveloper IMF Tune

Working with the Exchange Management Shell

Kenneth Spiteri

Kenneth Spiteri Photo

Kenneth is an Exchange Administrator who loves to share anything he finds interesting with the rest of the community. He also helps with the administration of the site.

Cast your Vote
Poor Excellent

The Exchange Management Shell provides a powerful administrative interface to the various Exchange 2007 configuration objects. Today we learn using this interface with the help of various practical examples.

The Exchange Management Shell is a powerful command line environment that builds on the Windows PowerShell framework. Included with Exchange 2007, it allows you to manage any aspect of Exchange that you can manage from the Exchange Management Console. In fact, every time you make a change from the Management Console, behind the scenes a shell command is being executed. When working in the Exchange Management Shell, as well as Exchange specific commands, all Windows PowerShell commands are available too.

To launch the Exchange Management Shell, go to Start > Programs > Microsoft Exchange Server 2007 > Exchange Management Shell. A command prompt like window will be spawned, similar to the one shown in the image below:

Exchange Management Shell

Whilst working in the shell, the "get-help" command will become invaluable as you learn the syntax and function of the various cmdlets. If you are unsure of which cmdlets are available for a specific server role, component or functionality you can return all cmdlets for any of these elements by calling the get-help command with the following parameters:

  • get-help -role <server_role>
  • get-help -component <component_name>
  • get-help -functionality <functionality_name>

To view a detailed description of a particular cmdlet, type "get-help <cmdlet_name>", as shown in the example in the image below:

Get-Help

To end the Exchange Management Shell session, type exit and press the return key.

Cmdlets

Cmdlets, or command-lets, are a set of commands for the configuration and management of Exchange Server 2007. They each have descriptive verb-noun names, where the verb describes the action that the cmdlet takes and the noun describes the object on which the action is being taken. For a full list of available cmdlets and which roles they apply to, visit:
http://technet.microsoft.com/en-us/library/bb123703.aspx

Some of the most common cmdlets you'll use for everyday management and administration will be the following:

Test-<noun> performs diagnostics tests against the specified object.
Get-<noun> retrieves information about the specified object.
Set-<noun> updates information for the specified object.
Enable-<noun> activates the object.
Disable-<noun> deactivates the object.
New-<noun> creates a new instance of the object.
Remove-<noun> deletes an instance of the object.

Tip: As you are typing your command, use the TAB key to shift through the available commands quickly and complete the command for you.

Pipelining (redirecting output)

Pipelining allows you to pass the output of one cmdlet as input into another cmdlet, so instead of you having to run a series of commands in sequence to get the desired output, you can put it all in a single line of command and have the shell do it for you. To do this, you must separate the cmdlets by the pipe (|) symbol. Here is an example that shows how to pipe output of the get-mailboxdatabase command into the get-mailbox command, resulting in only the information for the mailboxes in the "Sales" database to be displayed:

Get-Mailbox

Creating and running Scripts

The Exchange Management Shell gives you the flexibility to create your own scripts. To create a script, open a text editor (such as notepad), write the contents of the script and save it as a ".ps1" file. To execute a script, from the Exchange Management Shell, type the full path of your script.

For example:
C:\pshellscripts\queue.ps1

This will execute the queue.ps1 script from the C:\pshellscripts folder which, for the purposes of this example, simply contained a cmdlet to obtain queue information (the get-queue command).

Note: The default location of the Exchange Management Shell scripts is C:\Program Files\Microsoft\Exchange Server\Scripts.

Cmdlet examples for common Administrative Tasks

Below are some cmdlet examples of common administrative tasks that you can run at the Exchange Management Shell.

Creating a Distribution list:

New-DistributionGroup -Name "Support" -OrganizationalUnit "testdomain.local/Support" -SamAccountName "Support" -Type "Distribution"

Creating a mail-enabled User:

New-MailUser -Name "Kenneth Spiteri" -ExternalEmailAddress "kenneths@testdomain.local" -UserPrincipleName "Kenneth@testdomain"

You will then be asked to enter a password for this user.

Moving mailboxes between databases - this example shows how to move Kenneth's mailbox to a new mailbox database called Sales:

Move-Mailbox kenneths@testddomain.local -TargetDatabase Sales

Creating a new Journal Rule - This example shows how to create a new journal rule that applies to all e-mail messages that pass through the Hub Transport server:

New-JournalRule -Name "Journal All" -JournalEmailAddress "mx2007 Journal Mbx" -Scope Global -Enabled $True

View information about Hub Transport or Edge Transport server Queues - This example shows how to obtain queue information for the local server where the queue contains more than 50 messages, and then piped to a Format-List command so the result is displayed as a detailed list:

Get-Queue -Filter {MessageCount -gt 50} | Format-List

Importing PST files into a Mailbox

Before you start, it is important to note that to run the Import-Mailbox cmdlet, the account you use must have full access to the target mailbox, form part of the local Administrators group on the target server and be delegated the Exchange Server Administrator role. You will also need to run the Import-Mailbox cmdlet from a 32-bit computer that has the 32-bit version of the Exchange management tools and Microsoft Office Outlook 2003 Service Pack 2 or later installed. To download the 32-bit version of the Exchange 2007 management tools, go here: http://go.microsoft.com/fwlink/?LinkId=82335.

This example shows how to import the e-mails from a set of PST files into the respective mailboxes for a given date period:

Dir C:\PST\*.pst | Import-Mailbox -StartDate 01/01/2008 -EndDate 12/31/2008

Note that the ".pst" files must each be named <alias>.pst.

For more information about the Import-Mailbox cmdlet check Replacing ExMerge by Import-Mailbox and Export-Mailbox.

Adding permissions to a Mailbox - This example will show how to grant full access mailbox permissions to Kenneth Spiteri on Alex Zammit's mailbox:

Add-MailboxPermission "Alex Zammit" -User kenneths -Accessright Fullaccess -InheritanceType all

Changing the priority of a Transport Agent

Set-TransportAgent "CompanyXYZ AntiSpam Agent" -Priority 2

Tip: To view a list of Transport Agents installed on your machine execute the Get-TransportAgent cmdlet.

Retrieving information from the Message Tracking Log - This example shows how to obtain the message tracking log information for a given period for the user Alex Zammit:

Get-MessageTrackingLog -Start "06/03/2009 9:00AM" -End "06/03/2009 6:00PM" -Sender "alexz@testdomain.local"

Testing the connectivity of Outlook Web Access

This example shows how to test the connectivity of a given Outlook Web Access URL using the credentials obtained of the testdomain\Administrator user:

Test-OwaConnectivity -URL:https://mx2007/owa -MailboxCredential:(get-credential testdomain\Administrator)

This example shows how to test all Outlook Web Access virtual directories (including those that do not require SSL):

Test-OwaConnectivity -ClientAccessServer:mx2007 -AllowUnsecureAccess

Conclusion

This article has shown how the Exchange Management Shell is a simple yet powerful tool that gives greater flexibility and helps getting things done faster. To learn more check the references that follow.

References

Introduction to the Exchange Management Shell (PDF)

Exchange Management Shell Quick Reference

Windows PowerShell Team Blog

PowerShell.com

Copyright © 2005 - 2024 All rights reserved. ExchangeInbox.com is not affiliated with Microsoft Corporation