Book Image

Microsoft Exchange Server 2013 PowerShell Cookbook: Second Edition - Second Edition

Book Image

Microsoft Exchange Server 2013 PowerShell Cookbook: Second Edition - Second Edition

Overview of this book

Microsoft Exchange Server 2013 is a complex messaging system. Windows PowerShell 3 can be used in conjunction with Exchange Server 2013 to automate and manage routine and complex tasks to save time, money, and eliminate errors.Microsoft Exchange Server 2013 PowerShell Cookbook: Second Edition offers more than 120 recipes and solutions to everyday problems and tasks encountered in the management and administration of Exchange Server. If you want to write scripts that help you create mailboxes, monitor server resources, and generate detailed reports, then this Cookbook is for you. This practical guide to Powershell and Exchange Server 2013 will help you automate and manage time-consuming and reoccurring tasks quickly and efficiently. Starting by going through key PowerShell concepts and the Exchange Management Shell, this book will get you automating tasks that used to take hours in no time.With practical recipes on the management of recipients and mailboxes as well as distribution groups and address lists, this book will save you countless hours on repetitive tasks. Diving deeper, you will then manage your mailbox database, client access, and your transport servers with simple but effective scripts.This book finishes with advanced recipes on Exchange Server problems such as server monitoring as well as maintaining high availability and security. If you want to control every aspect of Exchange Server 2013 and learn how to save time with PowerShell, then this cookbook is for you.
Table of Contents (23 chapters)
Microsoft Exchange Server 2013 PowerShell Cookbook
Credits
About the Authors
Acknowledgement
About the Reviewers
www.PacktPub.com
Preface
Index

Formatting output


One of the most common PowerShell questions is how to get information returned from commands in the desired output on the screen. In this recipe, we'll take a look at how you can output data from commands and format that information for viewing on the screen.

How to do it...

To change the default output and view the properties of an object in list format, pipe the command to the Format-List cmdlet:

Get-Mailbox testuser | Format-List

To view specific properties in table format, supply a comma-separated list of property names as parameters, as shown next when using Format-Table:

Get-Mailbox testuser | Format-Table name,alias

How it works...

When you run the Get-Mailbox cmdlet, you only see the Name, Alias, ServerName, and ProhibitSendQuota properties of each mailbox in a table format. This is because the Get-Mailbox cmdlet receives its formatting instructions from the exchange.format.ps1xml file located in the Exchange server bin directory.

PowerShell cmdlets use a variety of formatting files that usually include a default view with only a small subset of predefined properties. When you need to override the default view, you can use Format-List and Format-Table cmdlets.

You can also select specific properties with Format-List, just as we saw when using the Format-Table cmdlet. The difference is, of course, that the output will be displayed in list format.

Let's take a look at the output from the Format-Table cmdlet, as shown previously:

As you can see here, we get both properties of the mailbox formatted as a table.

When using Format-Table cmdlet, you may find it useful to use the -Autosize parameter to organize the columns based on the width of the data:

This command selects the same properties as our previous example, but this time we are using the -Autosize parameter and the columns are adjusted to use only as much space on the screen as is needed. Remember, you can use the ft alias instead of typing the entire Format-Table cmdlet name. You can also use the fl alias for the Format-List cmdlet. Both of these aliases can keep your commands concise and are very convenient when working interactively in the shell.

There's more…

One thing to keep in mind is that you never want to use the Format-* cmdlets in the middle of a pipeline since most other cmdlets will not understand what to do with the output. The Format-* cmdlets should normally be the last thing you do in a command unless you are sending the output to a printer or a text file.

To send formatted output to a text file, you can use the Out-File cmdlet. In the following command, the Format-List cmdlet uses the asterisk (*) character as a wildcard and exports all of the property values for the mailbox to a text file:

Get-Mailbox testuser | fl * | Out-File c:\mb.txt

To add data to the end of an existing file, use the -Append parameter with the Out-File cmdlet. Even though we're using the Out-File cmdlet here, the traditional cmd output redirection operators such as > and >> can still be used. The difference is that the cmdlet gives you a little more control over the output method and provides parameters for tasks, including setting the encoding of the file.

You can sort the output of a command using the Sort-Object cmdlet. For example, this command will display all mailbox databases in alphabetical order:

Get-MailboxDatabase | sort name | ft name

We are using the sort alias for the Sort-Object cmdlet specifying name as the property we want to sort. To reverse the sort order, use the descending switch parameter:

Get-MailboxDatabase | sort name -desc | ft name

See also

  • The Understanding the pipeline recipe

  • The Exporting reports to text and CSV files recipe in Chapter 2, Exchange Management Shell Common Tasks