I have been doing some training on PowerShell Scripting this week and am going to be posting a number of articles on what I have been training on. This article deal with formatting your data output from your custom script or function to be viewed the way that you want it.
Have you ever run a cmdlet where the column width is not wide enough and your data get’s truncated? Well, here is a method that you can use to make the default output of your function or script display how you want it to.
The best way to do this is by using an existing xml formatting file as a template. Run the following PowerShell commands to access those templates:
1 2 | Set-Location $PSHOME Get-ChildItem | Where-Object -FilterScript {$_.name -match “.xml$”} |
Note: your custom view xml file will need to have the “.ps1xml” extension, which indicates that it is a “Windows Powershell XML Document”.
Now to creating the custom template. Let’s say you have the following function.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | function new-hmrcobject { $MyHashtable = @{‘column1’=”aaa”; ‘column2’=”bbb”; ‘column3’=”ccc” } $obj = New-Object -TypeName psobject -property $MyHashtable # Here we attach our object to a custom view called “hrmctoolcustomformat” $obj.PSObject.TypeNames.Insert(0,’hrmctoolcustomformat’) $obj } |
Now, create a custom view using the following file as a template:
C:\WINDOWS\system32\WindowsPowerShell\v1.0\DotNetTypes.format.ps1xml
Note: You can get a list of the format type template files that already comes with PS running the following command:
1 | Get-ChildItem | Where-Object -FilterScript {$_.name -match “.format.ps1xml”} |
Sample Output:
Mode LastWriteTime Length Name
—- ————- —— —-
-a— 10/06/2009 21:41 27338 Certificate.format.ps1xml
-a— 10/06/2009 21:41 27106 Diagnostics.Format.ps1xml
-a— 23/07/2012 19:12 144442 DotNetTypes.format.ps1xml
-a— 23/07/2012 19:12 14502 Event.Format.ps1xml
-a— 23/07/2012 19:12 21293 FileSystem.format.ps1xml
-a— 23/07/2012 19:12 287938 Help.format.ps1xml
-a— 23/07/2012 19:12 97880 HelpV3.format.ps1xml
-a— 23/07/2012 19:12 101824 PowerShellCore.format.ps1xml
-a— 10/06/2009 21:41 18612 PowerShellTrace.format.ps1xml
-a— 23/07/2012 19:12 13659 Registry.format.ps1xml
-a— 23/07/2012 19:12 17731 WSMan.Format.ps1xml
Remember: A custom view must always end with “.format.ps1xml”
We will use DotNetTypes.format.ps1xml as a template. Using this file, create a file called “hrmctools.formatps1xml”. That file will contain the following information modified from the template:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | <?xml version="1.0" encoding="utf-8" ?> <Configuration> <ViewDefinitions> <View> <Name>hrmctoolcustomformat</Name> # This must match with the “insert” command above so that object is linked to this format <ViewSelectedBy> <TypeName>hrmctoolcustomformat</TypeName> # This must match with the “insert” command above so that object is linked to this format </ViewSelectedBy> <TableControl> <TableHeaders> <TableColumnHeader> <Label>columnX</Label> # This displays a different name rather than defaulting to the property name. <Width>40</Width> # This defines the column width. </TableColumnHeader> <TableColumnHeader> <Label>columnY</Label> <Width>15</Width> </TableColumnHeader> <TableColumnHeader> <Label>columnZ</Label> <Width>15</Width> </TableColumnHeader> </TableHeaders> <TableRowEntries> <TableRowEntry> <TableColumnItems> <TableColumnItem> <PropertyName>column1</PropertyName> # Here you can re-arrange the column ordering. </TableColumnItem> <TableColumnItem> <PropertyName>column2</PropertyName> </TableColumnItem> <TableColumnItem> <PropertyName>column3</PropertyName> </TableColumnItem> </TableColumnItems> </TableRowEntry> </TableRowEntries> </TableControl> </View> </ViewDefinitions> </Configuration> |
NOTE: In PS, the content of xml files are always CASE SENSITIVE!!!
Once you have created your own custom view you then need to tell PowerShell to apply the formatting by using the cmdlet Update-FormatData:
1 | Update-FormatData -PrependPath "Path To Your XML File" |
Once you run the above command, this custom format you created should now be loaded into memory. You can verify this by using the Get-FormatData cmdlet:
1 | Get-FormatData |
If you have not done so in your function or script, you can attach your custom view to your function or script using the “insert” method:
$MyObject.PSObject.TypeNames.Insert(0,’hrmctoolcustomformat’)
Note*: This code has already been inserted into our function listed in this example.
NOTE**:The first parameter “0” is something that you always type in. You can now confirm that the object has successfully been attached to the custom view, by typing in PowerShell:
1 | $MyObject | Get-Member |
Also if you output your object, you should now notice that its appearance should have now changed to those that you defined in the custom view.
A “Typename” is essentially a name that you give to your object. It tells PowerShell the type of object that it is. Know that it is possible for a number of commands to have outputting objects of the same type (the same typename value). This could affect other cmdlets and functions in your script, so be sure to debug if necessary.
HAPPY SCRIPTING!
MORE TO COME!!
REFERENCES:
Creating Custom Format Views