2. Formatting Output
If you look at the output of the Get-Mailbox cmdlet shown in Figure 1,
you might be tempted to think that the output capabilities of
PowerShell are limited, but this is far from the truth. The output
shown in Figure 1 was the default output for the Get-Mailbox cmdlet. The programmer decided that the output should be in a formatted table with the name, alias, home server, and ProhibitSendQuota properties as columns. However, you can select on your own the properties you want by merely piping the output of the Get-Mailbox cmdlet to either the Format-Table (FT for short) or Select cmdlet:
Get-Mailbox | FT Name,ProhibitSendQuota,ProhibitSendReceiveQuota
Figure 2 shows the output of the preceding command.
The output of the Get-Mailbox cmdlet was directed to the Format-Table or FT cmdlet; the result was columns for the Name, ProhibitSendQuota, and ProhibitSendReceiveQuota limits.
You may be wondering how you can learn all the properties of an object. The default output of the Get-Mailbox
cmdlet, for example, is probably not the most useful for your
organization.
When you direct the output of a cmdlet such as Get-Mailbox to the Format-List cmdlet, you will see all the properties for that object. Figure 3 shows an example where we have directed the output of a Get-Mailbox cmdlet to the FL (Format-List) cmdlet. You will notice in Figure 3
that the properties filled up more than one screen. However, you will
find that outputting all the properties of an object using the Format-List cmdlet is very useful if you need to know specific property names.
The command we used is as follows:
Get-Mailbox "Clayton Kamiya" | Format-List
3. Directing Output to Other Cmdlets
You have already seen a couple of examples where we
used the pipe symbol (|) to direct the output of one command to be used
as input for the next command, such as Get-Mailbox | Format-Table.
You can do this because PowerShell commands act on objects, not just
text. Unlike with other shells or scripting languages, you don't have
to use string commands or variables to pass data from one command to
another. The result is that you can use a single line to perform a
query and complex task — something that might have required hundreds of
lines of programming in the past.
One of our favorite examples would be making
specific changes to a group of people's mailboxes. Let's say you need
to ensure that all executives in your organization should be able to
send and receive a message that is up to 50 MB in size rather than the
default 10 MB to which the system limits the user. Earlier we showed
you how you could get the properties of the mailbox that you were
interested in, such as the MaxSendSize and MaxReceiveSize properties.
First, let's use the Get-DistributionGroupMember cmdlet to retrieve the members of the Executives distribution group:
Get-DistributionGroupMember "Executives"
Name RecipientType
---- -------------
Mark Watts MailboxUser
David Elfassy MailboxUser
Brian Tirch MailboxUser
Paul Robichaux MailboxUser
Devin Ganger MailboxUser
Julie Samante MailboxUser
Cynthia Wang MailboxUser
Remember that though you see the text listing of the
group members, what is actually output are objects representing each of
the members.
It is important to note that while piping the output
of one cmdlet as input for another cmdlet works frequently, it does not
work all the time. Piping input to a cmdlet will always work when the
noun used by the two cmdlets is the same, such as this:
Get-Mailbox -Server HNLMBX01 | Set-Mailbox -CustomAttribute1 "I am on a
great server!"
For cmdlets that do not support piping between them, you can usually use a trick such as using the foreach cmdlet to process the data.
So, now let's pipe the output of that cmdlet to the Set-Mailbox
cmdlet and do some real work! To change the maximum incoming and
outgoing message size for the members of the Executives group, you
would type the following command:
Get-DistributionGroupMember "#Executives" | Set-Mailbox
-MaxSendSize:50MB -MaxReceiveSize:50MB
-UseDatabaseRetentionDefaults:$False
Notice that the Set-Mailbox cmdlet did not require any input because it will take as input the objects that are output from Get-DistributionGroupMember.
When you run these two commands, there will be no output unless you
have specified other options. But you can easily check the results by
requesting the membership of the Executives group, piping that to the Get-Mailbox cmdlet, and then piping that output to the Format-Table cmdlet, as shown here:
Get-DistributionGroupMember "#Executives" | Get-Mailbox | Format-Table
Name,MaxSendSize,MaxReceiveSize
Name MaxSendSize MaxReceiveSize
---- ----------- --------------
Clarence A. Birtcil 50 MB (52,428,800 bytes) 50 MB (52,428,800 bytes)
Cheryl Tung 50 MB (52,428,800 bytes) 50 MB (52,428,800 bytes)
Jonathan Core 50 MB (52,428,800 bytes) 50 MB (52,428,800 bytes)
Kevin Wile 50 MB (52,428,800 bytes) 50 MB (52,428,800 bytes)
Julie R. Samante 50 MB (52,428,800 bytes) 50 MB (52,428,800 bytes)
Gerald Nakata 50 MB (52,428,800 bytes) 50 MB (52,428,800 bytes)
Pretty cool, eh? After just a few minutes
working with PowerShell and the EMS extensions, we hope that you will
be as pleased with the ease of use as we are.