Вы находитесь на странице: 1из 26

Module 2

Understanding and Using


the Formatting System

Module Overview
List and explain the rules that the shell follows when

converting elements into a text display

Explain what cmdlets can successfully receive the output

of a Format- cmdlet

Identify the configuration files used to specify the shells

default formatting

Identify the general procedure used to provide custom

formatting instructions in XML files

Format cmdlet output objects using the

various Format- cmdlets

Format cmdlet output objects in a table,

including calculated columns

Convert objects to an HTML representation

Lesson 1: Understanding the Formatting System


List and explain the rules that the shell follows when

converting elements into a text display

Explain what cmdlets can successfully receive the output

of a Format- cmdlet

Identify the configuration files used to specify the shells

default formatting

Formatting Defaults
Nearly every PowerShell cmdlet produces some sort of

element as its output

Elements contain data, often more data than is presented

Elements and their data can be manipulated

PowerShells use of elements enables you to customize the

formatting of data, without resorting to low-level text


manipulation

Elements are configured with default output that exposes


limited data

Elements can be instructed instead to provide more or less


data or to reformat that data into different output

Directly at the command line you can override defaults,

supplement defaults, or redirect output to new formats

To do this, youll need to follow a few rules

Rule 1: Is There a Predefined View?


For each displayed element, PowerShell will first check to

see if a predefined format view has been registered for the


type name

Views are defined in special XML configuration files

These files have a .format.ps1xml extension

To see these files, use this command:


Dir $pshome

The shell loads these files each time it starts, storing view
information for the duration of the shell session

Do not modify predefined views. They are digitally signed and


will not load if they have been modified in any way

Views contain formatting information, such as layout

customizations, column labels and widths, script code, etc.

Demonstration: Finding Format Files


See the predefined view for a specific element type name

Rule 2: Is There a Property Set?


Sometimes predefined views do not exist

When they dont, the shell looks to see if a


DefaultDisplayPropertySet type extension has been registered
for the type name

This type extension is also stored in an XML file, this time with
a .types.ps1xml extension

To see these files, again use this command:


Dir $pshome
Do not modify type extensions either. They are digitally

signed, and will not load if they have been modified in any
way

Many type extensions ship by default with PowerShell. You

can also create your own to customize your display


environment

Demonstration: Finding a DefaultDisplayPropertySet


See how the shell locates a DefaultDisplayPropertySet type

extension

Rule 3: Table or List?


Finally, the shell determines how many attributes it needs

to display

This can be derived from DefaultDisplayPropertySet

This can be derived from a predefined formatting view

Or, this can simply be all the elements attributes

Default behavior:

If the shell needs to display four or fewer attributes, it defaults


to using a table

If the shell needs to display five or more attributes, it default s


to using a list

Once this determination is made, the shell creates the final

display, internally calling Format-List or Format-Table

Out-Default
The Out-Default cmdlet

Is always a component of pipeline output, even if it isnt


specifically called

Sends whatever elements are in the pipeline to the default


display device. This device is typically Out-Host

Is the reason why cmdlet output appears in the console


window by default

As a result, these cmdlets are functionally the same:

Get-Process
Get-Process | Out-Default
Get-Process | Out-Host
You generally do not need to invoke Out-Default. However

you need to use other cmdlets (like Out-File) to redirect


output

Out- and Format The Out- cmdlets

Are not capable of understanding normal elements, such as


processes and services

Are designed to use special formatting elements, which are


produced by a Format- cmdlet

As a result, these cmdlets are functionally the same:

Get-Process
Get-Process | Out-Default | Out-Host | Format-Table | Out-Host
The formatting system internally determines which

Format- cmdlet is called. This determination is based on


the registered view for the element.

Caution: Once a Format- cmdlet executes, your original

output is lost. This happens because the cmdlet produces


a formatting element.

Demonstration: Output of a Format- Cmdlet


See the differences between typical cmdlet output and the

output of a Format- cmdlet

Multiple Views
PowerShell uses, by default, the first predefined view it

finds for a type name

However, you can manually specify an alternate

predefined view by using the view parameter:

Get-Process | Format-Table view priority

Important: You can reference only views that actually

exist. For the preceding command to work, a view called


priority must be defined for Format-Table

There is no way to list the available views for a type name

This is why you need to manually search .format.ps1xml files


to locate alternate views and learn their names

Lesson 2: Using the Formatting System


Identify the general procedure used to provide custom

formatting instructions in XML files

Format cmdlet output elements using the various Format-

cmdlets

Format cmdlet output elements in a table, including

calculated columns

Convert elements to an HTML representation

Customizing Defaults
You can create custom formatting views by creating your

own .format.ps1xml files. (Note: This process is beyond


the scope of this class.)

New files will register new or supplemental defaults, or new


named views in the shell

It is easiest to use Microsofts existing .format.ps1xml files

as examples for creating your own files

Remember: Do not modify the Microsoft-signed


.format.ps1xml files, or they will not load

Once a new file is created, you will use the Update-

FormatData cmdlet to load your file into the shell

This cmdlet registers the views in your file for the currentlyrunning shell session. It will need to be run in each session.

Alternatively, you can run this command within a PowerShell


profile script to automatically load it into each session. You will
learn more about profile scripts in a later module.

Demonstration: Customizing Defaults


See how to create and load a custom formatting file into

the shell

Using the Format Cmdlets


Creating custom .format.ps1xml files can be difficult
It can be easier to simply use existing parameters of the

Format- cmdlets to customize your needed output

Format-Table

Format-List

Format-Wide

Format-Custom

The help files for these cmdlets document their usage


Take time now to read through the full help files for
these four cmdlets.

Custom Columns
You can use Select-Object to add custom attributes to an

object.

For example , to add the ComputerName attribute to a


computer element that already had a Name attribute , use:

Get-ADComputer filter * |
Select
*,@{Label='ComputerName';Expression={$_.Name}}

This command gathers all of the original attributes through the


* wildcard, then adds a new one using Label

Expression defines the value that the attribute will contain


and curly braces are used to designate a script block

The special $_. placeholder stands in for whatever SelectObject was given in this case, a set of computer names

There are additional and very powerful uses of this syntax

in your student guide. Review those uses now because


you will use them regularly.

Demonstration: Customizing Formatting


Learn how to use Format- cmdlets to customize the output

of cmdlets

HTML Output
Sometimes, you want to format data so it can be viewed

in a Web browser

The ConvertTo-HTML cmdlet

Converts elements into an HTML-based table

Itself does not write HTML to a file, but instead places string
elements into the pipeline

You need to pipe its results to Out-File to create an HTML file


that can be viewed in the browser
Get-EventLog Security newest 20 |
ConvertTo-HTML | Out-File events.htm

Parameters of ConvertTo-HTML enable customization of

the resulting HTML page

Note: Do not pipe a Format- cmdlet to ConvertTo-HTML

Terminology Reminder
Remember that PowerShell uses formal terms to describe

the elements in the pipeline, as well as their attributes

The word element was used earlier to refer to things that a


cmdlet places in the pipeline

The more formal word for elements is objects

Objects have attributes, which are characteristics of that


objects

The more formal word for attributes is properties

These informal words were introduced earlier to ease your

introduction into PowerShell

However, you may want to use the formal words as you grow
in experience because it makes communication easier

Demonstration: ConvertTo-HTML
Learn how to convert information to HTML and display the

results in a Web browser

Lab: Using the Formatting Subsystem


Exercise 1: Displaying Calculated Properties
Exercise 2: Displaying a Limited Number of Columns
Exercise 3: Displaying All Properties and Values of Objects
Exercise 4: Viewing Objects via HTML
Exercise 5: Displaying a Limited Number of Properties
Exercise 6: Displaying Objects Using Different Formatting
Exercise 7: Displaying a Sorted List of Objects

Logon information
Virtual machine

LON-DC1

LON-SVR1

Logon user name

Contoso\Administrator

Contoso\Administrator

Password

Pa$$w0rd

Pa$$w0rd

Estimated time: 20 minutes

Lab Scenario
You are a system administrator for a company and work

with a team of developers.

Recently, some users have complained about an in-house

developed application. The developers have asked you to


monitor the processes associated with the troubled
application and let them have easy access to the data.

You have decided to provide them with that information

via an HTML-based page so they can easily view the


information in a Web browser.

You also want to customize your view of this information

as you view it in the PowerShell console.

Lab Review
It would be nice to be able to format the color of each row

in some cases. Does Format-Table appear to provide the


ability to change the color of rows?

What might happen if I used Select-Object in the pipeline,

after Ive used Format-Table (|format-table


<something>|select-object <something>)? Would that
work?

Module Review and Takeaways


What parameter can be used to keep Format-Table from

wasting space when only two or three columns are


displayed?

What happens when you try to display more table columns

than will fit on the screen? Is there a way to keep column


information from being truncated?

Вам также может понравиться