An Exchange admin with PowerShell skills is a valuable asset, especially as organizations move to Exchange Server 2010. Exchange Management Shell enables admins to manage every aspect of Exchange 2010. Even if you don’t consider yourself command-line savvy, don’t be intimidated. Now is the time to become familiar with Exchange Management Shell and PowerShell cmdlets.
EMS is built on top of Windows PowerShell 2.0. EMS and Windows PowerShell focus on managing objects in large groups, an act that traditionally had been difficult with GUI-based tools. When you have thousands of objects, it can be tough to find the three that you have to modify. Having a tool that can consistently find and edit those objects with minimal typing and interpretation is invaluable.
So, what is a cmdlet? At its simplest, a cmdlet is a combination of a verb and a noun. The verb might be an action like Get that will retrieve information about an object or objects. The noun is the object that the action is performed on, like a mailbox. Put the two together and you have:
Launch Exchange Management Shell in Exchange 2010 and type the cmdlet. In the output, EMS will list all the mailboxes in your organization. If you want to retrieve a list of sales mailboxes in your Exchange organization, you would use a command similar to:
Get-Mailbox -OrganizationalUnit Sales
Now, suppose you wanted to retrieve information for all mailboxes on a server called TampaMBX02. In that case, you would use a command similar to:
Get-Mailbox -Server TampaMBX02
Piping cmdlets together
Now that you understand the basics, let's go a little deeper. Suppose you want to prevent these sales mailboxes from sending and receiving mail after they reach 500 MB. To do so, you could set a 500 MB Prohibit Send quota on them. After you’ve collected the mailboxes you need, you just need to set the quota. To take the result set from one cmdlet and pass it to another cmdlet, you must use the pipe symbol (|). To accomplish this, simply put the two parts together as such:
Get-Mailbox -OrganizationalUnit Sales | Set-Mailbox -ProhibitSend 500MB -UseDatabaseQuotaDefaults $false
In this example, I appended the -UseDatabaseQuotaDefaults $false parameter. This ensures that database settings won’t be applied and won’t overwrite the recipient settings you're trying to configure.
As another example, suppose you wanted to give your executives unlimited mailbox quotas, but your execs are spread across multiple mailbox databases and multiple servers. You’d have to use the ProhibitSend quota and append it with a cmdlet for the distribution list associated with those executives:
Get-DistributionGroup Executives | Get-DistributionGroupMembership | Set-Mailbox -ProhibitSend Unlimited -UseDatabaseQuotaDefaults $false
In just a few moments, you’ve gone from a simple Get-Mailbox cmdlet to a fairly sophisticated one.
The WhatIf parameter
If you're still nervous that your scripting inexperience might harm Exchange, don't worry. The WhatIf parameter lets you observe potential changes to objects before making them. If you're satisfied with the results, you can use the same cmdlet in the PowerShell prompt, delete the WhatIf parameter and re-execute the cmdlet. For example, to move all mailboxes on a server to a database on a new server, use the New-MoveRequest cmdlet with the WhatIf parameter to check the mailboxes’ readiness before executing the move. The command looks like this:
Get-Mailbox -Server SearchExchangeEX1 | New-MoveRequest -TargetDatabase "SearchExchangeDB2" –WhatIf
The command simulates what would happen if you moved all mailboxes off the SearchExchange-EX1 server to the SearchExchangeDB2 database without actually making the move. If the mailboxes aren’t ready to be moved, you'll receive an error message. If there are no errors, execute the command again without the WhatIf parameter:
Get-Mailbox -Server SearchExchangeEX1 | New-MoveRequest -TargetDatabase "SearchExchangeDB2"
Tab completion for cmdlets
One very useful PowerShell shortcut once you've learned started writing commands is Tab completion, or Tab expansion, which lets you complete portions of cmdlets using the Tab key. Type the verb and a few characters of the noun and press Tab. For example, you could begin typing Get-M and press the Tab key until Get-Mailbox appears. If you accidentally tab past Get-Mailbox, just hold down the Shift key and press Tab repeatedly to move backward through the list of cmdlets until you reach the one you want.
Even though case is not important in PowerShell, tab completion also automatically corrects case. Type get-dist and press the Tab key. In my opinion, Get-DistributionGroupMembership reads much cleaner than get-distributiongroupmembership, though it is only a cosmetic change and they both accomplish the same thing.
ABOUT THE AUTHOR
Richard Robb has been a respected technical trainer on Microsoft technologies for the past thirteen years. Rick has earned a number of technical certifications including Microsoft Certified Trainer (MCT), Microsoft Certified Systems Engineer (MCSE) on Windows Server 2003, 2000 and NT 4.0, Microsoft Certified Systems Administrator (MCSA) on Windows Server 2003 and 2000, Microsoft Certified Desktop Support Technician (MCDST), as well as Microsoft Certified IT Professional (MCITP) for Exchange 2010, Exchange 2007, as well as Windows Server 2008.
Rick has delivered Exchange Server 2010 classes to top Fortune 500 companies, leading colleges and universities, as well as many governmental agencies in the US and Canada.