Custom formatting

When writing your own code and executing PowerShell statements, you might have noticed that the formatting changes from time to time. Cmdlets such as Get-Process display their results in a neat table, Get-ChildItem adds the parent directory to the formatted output, and so on.

PowerShell can be extended by your own custom formatting for all kinds of objects as well. This way you can set a template to be applied to, for example, all ADUser objects because you don't want the default list format. Or, you define the format for your own objects that your function returns.

Creating the formatting can be done by altering XML files, which can be exported from existing data or created new. Using the cmdlet Update-FormatData it is possible to import and apply the desired format. Prepending the custom format will allow you to override internal formatting for objects, while appending the data is useful for adding format data to new object types. These are most likely .NET types that you created yourself in a module for instance.

The following code sample changes the default formatting for objects returned by the Get-FileHash cmdlet:

# Retrieve existing data to modify it
$existingFormat = Get-FormatData -TypeName Microsoft.PowerShell.Commands.FileHashInfo
$existingFormat | Export-FormatData -Path .Microsoft.PowerShell.Commands.FileHashInfo.format.ps1xml -IncludeScriptBlock

# Notice the existing format. The Algorithm comes first
Get-FileHash .Microsoft.PowerShell.Commands.FileHashInfo.format.ps1xml

# Change the XML to display the path first, then the hash, then the algorithm
psedit .Microsoft.PowerShell.Commands.FileHashInfo.format.ps1xml

# Update data after modifications
# Prepending allows us to override the existing data
# Appending is more suited to new object types not already formatted
Update-FormatData -PrependPath .Microsoft.PowerShell.Commands.FileHashInfo.format.ps1xml

# Notice the changed format
Get-FileHash .Microsoft.PowerShell.Commands.FileHashInfo.format.ps1xml

The following screenshot shows how the format has been changed by applying custom formatting. The bottom part of the image displays the original format while the top half displays the modified format.

When editing or creating new views, you have the option to specify a layout through XML nodes called TableControl, ListControl, and WideControl, each corresponding to their type. If you recall the cmdlets Format-Table, Format-List, and Format-Wide, this is where the controls are being used:

$formatXml = @"
<Configuration>
<ViewDefinitions>
<View>
<Name>System.Diagnostics.Process</Name>
<ViewSelectedBy>
<TypeName>System.Diagnostics.Process</TypeName>
</ViewSelectedBy>
<TableControl>
<AutoSize />
<TableHeaders>
<TableColumnHeader>
<Label>Name</Label>
</TableColumnHeader>
<TableColumnHeader>
<Label>WS in MB</Label>
</TableColumnHeader>
</TableHeaders>
<TableRowEntries>
<TableRowEntry>
<TableColumnItems>
<TableColumnItem>
<PropertyName>ProcessName</PropertyName>
</TableColumnItem>
<TableColumnItem>
<ScriptBlock>[Math]::Round(($_.WorkingSet64 / 1MB),2)</ScriptBlock>
</TableColumnItem>
</TableColumnItems>
</TableRowEntry>
</TableRowEntries>
</TableControl>
</View>
</ViewDefinitions>
</Configuration>
"@

$formatXml | Out-File .MyCustomFormat.ps1xml
Update-FormatData -PrependPath .MyCustomFormat.ps1xml
Get-Process -Id $Pid

Within each control node, you can define headers for your rows and columns, and define, for example, the table column width, and which object property is displayed where. In order to format the data that is displayed, script blocks can be used instead of the property names. In the following example, the working set is expressed in MB with two decimal places:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.140.242.165