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
- Understanding the pipeline
- Exporting reports to text and CSV files in Tasks