PowerShell Formatting Deep Dive

Thomas Lee ([email protected])
PowerShell MVP
 PowerShell MVP
 Worked with, trained and written about PowerShell
for years!
 SME on 6434 and TR on 10325 official MOC classes
 I have 25 copies of PowerShell Plus
 http://www.idera.com/Products/PowerShell/PowerShell-
Plus/ or
 http://tinyurl.com/psplus
 To win:
You need to email me with a code word.
2. Email me at [email protected]
3. The code word will be at the end of the presentation
 Know a bit (or more) about PowerShell
 At least to the level of the earlier Summit session!
 You know what a cmdlet is, what the pipeline does and
what objects are
 And you want to do more with formatting
 If you want to type-along with me, feel free!
 Formatting by default
 Formatting using Format-table, Format-List
 Using .NET format Strings
 .ToString()
 Formatting using Hash Tables
 Formatting XML
 Other output mechanisms
 Some thoughts on style etc
 PowerShell formats output by default
 Useful for simple queries
 Formatting is controlled by Format.PS1XML files
 Seven are installed by default
 LS $PSHOME\*.Format.PS1.XML
 You can
 You can edit built in Format.PS1XML files - but don’t…
 Instead, add new ones and ‘install’ in Profile
 The default formatting process
Cmdlet or
Format-Table or
Formatted Output
On Console
 User Defined Formatting
Cmdlet or
Format-Table or
Formatted Output
On Console
 Other Options
Cmdlet or
Text, CSV, XML
 Cmdlets take objects from pipeline – create table/list
 Default contents and ‘shape’ determined by .ps1xml
 You can override defaults, for example:
 GWMI win32_ComputerSystem | fl name,model
 Can also take a hash table for precise format control
 Used to print a single property as a table
 The Format-* cmdlets format objects
 Om the pipeline or via –Inputobject parameter
 You can adjust the details of how you want your data to
appear using the cmdlets, hash tables, format strings, etc
 They produce format objects
 The Out-* cmdlets take format objects and output it
to the console (and elsewhere)
 Format-Table
 Waits till ALL objects are ready before formatting and autosizes the width of each column
 -HidetableHeaders
 Hides table headers
 Format-List and Format-Table
 But you need to sort first
 Objects in the pipeline are sent to Out-Default
 Out-Default looks at the objects in the pipeline
 If formatting instructions – send it to Out-Host
 If objects - send to Format-List or Format-Table
 Out-Default looks in .PS1XML for guidance
 Select 1st view for guidance
 Five or less properties – call Format-Table
 Otherwise call Format-List
 This creates format objects
 Sent to Out-Default and then to the console
 You can get basic output from PoweShell using
default formatting
 You can see more/different using Format-table/list
 FL/FT typically used at the end of a pipeline
 But PowerShell is built on .NET
 Lets leverage .NET formatting
 Composite String – with placeholders
 "There are{0} ps1 files" –f
(ls C:\foo\*.ps1).count
 Placeholder Syntax
 {index[,alignment]{:formatstring}]
 Simple placeholder examples
 {0}
 {0,20}
 {0,-20:n}
 Use these along with the –f operator
 .NET formats values into the composite string!
 Fixed decimal point
 $a=123.4567898
 "The Number is: {0:#.000}" –f $a
 The Number is: 123.456
 Currency
 $a=123456.789123
 "{0:c2}" –f $a
 £123,456.79
 Phone Number
 $a=4255551212
 "{0:(###) ###-####}" -f $a
 (425) 555-1212
 All types have a .ToString() method
 Inherited from [Object]
 Base types have power over how to format
 Pass a format string to .ToString() to control
 Example
 $i = 23.123456
 $i.tostring("N2")
 23.12
 Format output for a single related set of values
 Number of files
 Number of files and their total size
 Also use it to format one line in a table
 Foreach ($file in $files) {display some aspect(s) of each file}
 .NET Formatting string used in
 Composite format string
 ToString()
 Hash Tables (later in this talk)
 Numeric format strings
 http://msdn.microsoft.com/en-us/library/427bttx3(VS.71).aspx
 Date and time format strings
 http://msdn.microsoft.com/en-us/library/97x6twsz(VS.71).aspx
 Enumeration format strings
 http://msdn.microsoft.com/en-us/library/c3s1ez6e(VS.71).aspx
Simple Formatting
 Using FT/FL, you can create a ‘calculated field’
 Field defined by a Hash Table with pre-defined key
(or Label) - <string>
Expression - <string> or <script block>
FormatString - <string>
Width <int32>
Alignment - value can be "Left", "Center",
 Name
or "Right")
 Send FT/FL hash table vs a property name
[email protected]{label="Process Name";
[email protected]{label="CPU Used";
Get-Process notepad | Format-Table $x1,$x2 -auto
[email protected]{label="Process Name";Expression={$_.name};
[email protected]{label="CPU Used"; Expression={$_.CPU};FormatString="N3"}
Get-Process | Format-Table $x1,$x2 –autosize
[email protected]{label="Process Name"; Expression={$_.name}}
Get-Process | Format-List $x3,$x2
 Give FT/FL better column/table headers
 Sometimes property name is ugly/unhelpful
 Display a calculated value
 Covert a number into gb/mb etc
 Expression = {$_.Length/1GB}
 Coerce field into to nicer format easily
 Import-CliXML
 Export-CliXML
 ConvertTo-XML
 Export-CSV (and Import-CSV)
 Useful for interop with XML Applications
 Uses WPF
 You need latest .NET Framework
 Creates sortable list in a separate window with
to help limit output
(and PowerShell ISE) need .NET 3.51
 The rest of PowerShell requires .NET 2.0
Out-Gridview and ISE use WPF
 By default, Out-Gridview displays same columns as FT
 Clone the object first to see all object properties
 Get-Process | Select-Object * | Out-Gridview
Hash tables and FT/FL
 Seen default formatting
 Used FT/FL and hash tables
 Formatted using .NET
 So let’s do create custom XML
 Two types of ps1xml
 Define/update types - *.types.ps1xml
 Define/update formatting - *.format.ps1xml
 Default Format XML shipped with PowerShell
 Apps , OS additions, modules add to this default set
 Stored in $PSHOME
 They are signed – editing breaks that!
 Copy and edit them instead
 Don’t like the way PowerShell formats a type?
 Develop your own display XML
 PowerShell ships with 7 format.ps1xml files
 You can write your own
 Define four views: table, list, wide, and complex
 Do NOT edit existing files – make a copy and edit it
 Run Update-FormatData to add new view
 Add this to $profile to persist the change
 Identifies the name of the view.
 Specifies the object type or types to which the view
 Specifies how items in the view should be combined in
<TableControl> <ListControl> <WideControl> <ComplexControl>
 Contain the tags that specify how each item is displayed.
Add-Type @'
public class aeroplane
public string Model =
public int
public int
Range =
public int
"Boeing 737";
= 12;
 But what about the display?
 Use FormatData.ps1xml
<View> <name>Airplane</name>
<View> <name>Airplane</name>
Saved as
Imported as needed/when needed
 Save XML to C
Update-FormatData c:\foo\aeroplane.format.ps1xml
 Then:
$Object = new-object aeroplane
$object | fl
 You can also extend existing types or add new ones
 Create XML file describing your addition
 Use Update-TypeData to incorporate it
 Persist the change by updating $Profile
if (($global:MSDNViewer -eq $null) -or ($global:MSDNViewer.HWND -eq $null)) {
$global:MSDNViewer = new-object -ComObject InterNetExplorer.Application}
$Uri = "http://msdn2.microsoft.com/library/" + $this.GetType().FullName + ".ASPX"
$global:MSDNViewer.Visible = $TRUE
 Create the XML
 On your workstation
 XCOPY during logon script?
 Store it somewhere useful
 C:\foo
 Somewhere else??
 Add the type/format information to your $Profile
 Update-TypeData
-Path <path>
 Update-FormatData –Path <path>
 Formatting can be simple or complex
 Lots of alternatives – have it your way
 Keep it simple and extend to meet your needs
 I have free copies of PowerShell Pro
 Idera’s PowerShell Development Tool
 The first 25 folks to email me will get a link to
download the software (and a license).
 I gave you the email address at the start of this
 The codeword is “POWERSHELLISAWESOME!”
 The first 25 to mail that code word to the address I
gave earlier will win their prize!
 Adding .MSDN() to types
 http://blogs.msdn.com/powershell/archive/2006/06/24/644987
 Formatting - see TFL’s articles on formatting
 http://tfl09.blogspot.com/2010/02/formatting-with-
 .NET Formatting
 http://msdn.microsoft.com/en-us/library/txafckwd(VS.71).aspx
 Thomas Lee’s Blogs/PowerShell site
 http://tfl09.blogspot.com
 http://pshscripts.blogspot.com
 http://www.reskit.net/psmc
Related flashcards
Computer science

25 Cards


43 Cards

Borland software

18 Cards

Duke Nukem

15 Cards

Create flashcards