In the previous articles in this series we looked at the basic aspects of PowerShell and then the underlying mechanisms that are used to connect both Exchange 2007 and Exchange 2010 management tools to Exchange. These articles can be found as follows:
- How To Maximize Exchange Administrator Productivity With PowerShell - Part 1
- How To Maximize Exchange Administrator Productivity With PowerShell - Part 2
Now that the basics and plumbing are out of the way what are some of the more interesting things that we can do?
Let’s take the most ubiquitous cmdlet, Get-Mailbox and expand upon it.
Limited View Of Mailboxes
The biggest issue I hear from customers is that they do not see all mailboxes by default, and this happens in a couple of scenarios. Firstly, Exchange will return a maximum of 1,000 mailboxes by default. This behaviour can be altered by specifying the following option.
-ResultSize Unlimited
Secondly, Exchange will only show you mailboxes from the current domain by default. Should you want to see mailboxes for all domains in the forest you will need to change the following:
Exchange 2007
$ADAdminSessionSettings –ViewEntireForest $TRUE
Exchange2010
Set-ADServerSettings –ViewEntireForest $TRUE
Alternatively specify the IgnoreDefaultScope parameter which tells PowerShell to ignore the default scope for the session and to use the entire forest as the scope.
Positional Parameter
You will recall that get-Mailbox can be used for something as simple as:
Get-Mailbox <user>
The <user> is a positional parameter that lets you specify the parameter's value without specifying the parameter's name. A parameter is a positional parameter if the Parameter Position
attribute is an integer. This integer indicates the position on the command line where the cmdlet can find the parameter's value. For more information about the various attributes that make up a parameter, see the Parameter Details section in Exchange Help.
Wildcard Searches
Get-Mailbox will also support wildcard searches so that you can return mailboxes that being with the phrase “User”
Get-Mailbox User*
All mailboxes beginning with the letter A:
Get-Mailbox A*
Mailboxes By Database
To show all mailboxes that are in a given database:
Get-Mailbox –Database
Mailboxes By Organisational Unit
To show all mailboxes in a given Organisational Unit (OU) in Active Directory:
Get-Mailbox –OrganizationalUnit Montreal-Staff-OU
Integrated Sort
The Get-Mailbox cmdlet also has the SortBy option so that you can sort the mailboxes without having to pipe to Sort-Object. Note that you can sort by only one attribute at a time. You can sort by the following attributes:
- Alias
- Display name
- Name
The results are sorted in ascending order.
Mailbox Type
To see only a particular type of mailbox the RecipientTypeDetails parameter can be used. Can then choose to show only mailboxes in the following classes:
RoomMailbox
EquipmentMailbox
LegacyMailbox
LinkedMailbox
UserMailbox
DiscoveryMailbox (Only applicable to Exchange 2010)
SharedMailbox
Return Archive Information
To see information about an archive enabled mailbox on Exchange 2010, simple add the Archive parameter:
Get-Mailbox user1 -Archive
If the archive should be located in Exchange Online, then use the –RemoteArchive parameter instead.
Filter Information
Perhaps the most interesting capability is Filter, which allows the administrator to specify an OPATH filter string to filter the returned mailboxes. It is well worth learning this capability as it is used in the following cmdlets:
- Get-CASMailbox
- Get-Contact
- Get-DistributionGroup
- Get-DynamicDistributionGroup
- Get-Group
- Get-Mailbox
- Get-MailContact
- Get-MailPublicFolder
- Get-MailUser
- Get-Recipient
- Get-UMMailbox
- Get-User
The full list of allowed parameters are documented on TechNet.
For example, we can pull back only mailboxes that are of type UserMailbox. This is more efficient that retrieving every mailbox and then stuffing it down the pipeline to a Where-Object clause
Get-Mailbox –Server Server1 -ResultSize Unlimited -Filter {RecipientTypeDetails -eq 'UserMailbox'}
Another example of a filter, would be the following applied to a Dynamic DistributionGroup:
All mailboxes that are on Server1 and user in the Dallas office:
Set-DynamicDistributionGroup AllServer1-Dallas –RecipientFilter { ServerName –eq 'Server1' –and Office –eq 'Dallas'
The other important aspect of the filter parameter is that it has the ability to greatly improve the performance of one’s command. If a small number of records are retrieved prior to passing them down the pipeline then the entire process will complete in a far more efficient manner than returning unneeded records and then passing them down the pipeline.
For part one please click here.
For part two please click here.
Cheers,
Rhoderick