{"id":720,"date":"2020-04-22T11:05:33","date_gmt":"2020-04-22T16:05:33","guid":{"rendered":"http:\/\/itblog.ldlnet.net\/?p=720"},"modified":"2020-04-22T11:11:40","modified_gmt":"2020-04-22T16:11:40","slug":"powershell-how-to-create-a-custom-view-for-your-ps-output-objects","status":"publish","type":"post","link":"https:\/\/itblog.ldlnet.net\/index.php\/2020\/04\/22\/powershell-how-to-create-a-custom-view-for-your-ps-output-objects\/","title":{"rendered":"PowerShell &#8211; How to create a custom view for your PS Output Objects"},"content":{"rendered":"\n<p>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. <br>Have you ever run a cmdlet where the column width is not wide enough and your data get&#8217;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.<\/p>\n\n\n\n<p>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:<\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\" title=\"Accessing XML Files For Template Information\">Set-Location $PSHOME\nGet-ChildItem | Where-Object -FilterScript {$_.name -match \u201c.xml$\u201d}\n<\/pre>\n\n\n\n<p class=\"has-text-color has-small-font-size has-medium-pink-color\"><strong>Note: your custom view xml file will need to have the \u201c.ps1xml\u201d extension, which indicates that it is a \u201cWindows Powershell XML Document\u201d.<\/strong><\/p>\n\n\n\n<p>Now to creating the custom template. Let&#8217;s say you have the following function.<\/p>\n\n\n<pre class=\"lang:PowerShell nums:True\" title=\"Custom Formatting XML\">function new-hmrcobject \n\n{\n$MyHashtable = @{\u2018column1\u2019=\u201daaa\u201d;\n\u2018column2\u2019=\u201dbbb\u201d;\n\u2018column3\u2019=\u201dccc\u201d\n}\n\n$obj = New-Object -TypeName psobject -property $MyHashtable\n\n# Here we attach our object to a custom view called \u201chrmctoolcustomformat\u201d\n$obj.PSObject.TypeNames.Insert(0,\u2019hrmctoolcustomformat\u2019) \n\n$obj\n}\n<\/pre>\n\n\n\n<p>Now, create a custom view using the following file as a template:<\/p>\n\n\n\n<p class=\"has-text-color has-small-font-size has-purple-color\"><strong>C:\\WINDOWS\\system32\\WindowsPowerShell\\v1.0\\DotNetTypes.format.ps1xml<\/strong><\/p>\n\n\n\n<p class=\"has-text-color has-small-font-size has-medium-pink-color\"><strong>Note: You can get a list of the format type template files that already comes with PS running the following command:<\/strong><\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\" title=\"Accessing XML Formats For Template Information\">Get-ChildItem | Where-Object -FilterScript {$_.name -match \u201c.format.ps1xml\u201d}\n<\/pre>\n\n\n\n<p class=\"has-normal-font-size\"><strong>Sample Output:<\/strong><\/p>\n\n\n\n<p class=\"has-small-font-size\">Mode LastWriteTime Length Name<br>\u2014- \u2014\u2014\u2014\u2014- \u2014\u2014 \u2014-<br>-a\u2014 10\/06\/2009 21:41 27338 Certificate.format.ps1xml<br>-a\u2014 10\/06\/2009 21:41 27106 Diagnostics.Format.ps1xml<br>-a\u2014 23\/07\/2012 19:12 144442 DotNetTypes.format.ps1xml<br>-a\u2014 23\/07\/2012 19:12 14502 Event.Format.ps1xml<br>-a\u2014 23\/07\/2012 19:12 21293 FileSystem.format.ps1xml<br>-a\u2014 23\/07\/2012 19:12 287938 Help.format.ps1xml<br>-a\u2014 23\/07\/2012 19:12 97880 HelpV3.format.ps1xml<br>-a\u2014 23\/07\/2012 19:12 101824 PowerShellCore.format.ps1xml<br>-a\u2014 10\/06\/2009 21:41 18612 PowerShellTrace.format.ps1xml<br>-a\u2014 23\/07\/2012 19:12 13659 Registry.format.ps1xml<br>-a\u2014 23\/07\/2012 19:12 17731 WSMan.Format.ps1xml<\/p>\n\n\n\n<p class=\"has-text-color has-small-font-size has-medium-pink-color\"><strong>Remember: A custom view must always end with \u201c.format.ps1xml\u201d<\/strong><\/p>\n\n\n\n<p>We will use <strong>DotNetTypes.format.ps1xml <\/strong>as a template. Using this file, create a file called \u201chrmctools.formatps1xml\u201d. That file will contain the following information modified from the template:<\/p>\n\n\n<pre class=\"lang:XML nums:True\" title=\"Custom XML Formatting FIle\">\n<?xml version=\"1.0\" encoding=\"utf-8\" ?> \n<Configuration> \n    <ViewDefinitions> \n\t\t<View> \n            <Name>hrmctoolcustomformat<\/Name>\n\t\t\t# This must match with the &#8220;insert&#8221; command above so that object is linked to this format \n            <ViewSelectedBy> \n                <TypeName>hrmctoolcustomformat<\/TypeName>\n\t\t\t\t# This must match with the &#8220;insert&#8221; command above so that object is linked to this format \n            <\/ViewSelectedBy> \n             <TableControl> \n                <TableHeaders> \n                    <TableColumnHeader> \n                        <Label>columnX<\/Label>\n\t\t\t\t\t\t# This displays a different name rather than defaulting to the property name. \n                        <Width>40<\/Width>\n\t\t\t\t\t\t# This defines the column width. \n                    <\/TableColumnHeader> \n\t\t\t\t\t<TableColumnHeader> \n                        <Label>columnY<\/Label> \n                        <Width>15<\/Width> \n                    <\/TableColumnHeader> \n                    <TableColumnHeader> \n\t\t\t\t\t\t<Label>columnZ<\/Label> \n                        <Width>15<\/Width> \n                    <\/TableColumnHeader> \n                <\/TableHeaders> \n                <TableRowEntries> \n                    <TableRowEntry> \n                        <TableColumnItems> \n                            <TableColumnItem> \n                                <PropertyName>column1<\/PropertyName>\n\t\t\t\t\t\t\t\t# Here you can re-arrange the column ordering. \n                            <\/TableColumnItem> \n                            <TableColumnItem> \n                                <PropertyName>column2<\/PropertyName> \n                            <\/TableColumnItem> \n\t\t\t\t\t\t\t<TableColumnItem> \n                                <PropertyName>column3<\/PropertyName> \n                            <\/TableColumnItem> \n                        <\/TableColumnItems> \n                    <\/TableRowEntry> \n                 <\/TableRowEntries> \n            <\/TableControl> \n        <\/View> \n    <\/ViewDefinitions> \n<\/Configuration> \n\n<\/pre>\n\n\n\n<div class=\"wp-block-file aligncenter\"><a href=\"https:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2020\/04\/SampleCustomXML.txt\">SampleCustomXML<\/a><a href=\"https:\/\/itblog.ldlnet.net\/wp-content\/uploads\/2020\/04\/SampleCustomXML.txt\" class=\"wp-block-file__button\" download>Download<\/a><\/div>\n\n\n\n<p class=\"has-text-color has-small-font-size has-medium-pink-color\"><strong>NOTE: In PS, the content of xml files are always CASE SENSITIVE!!!<\/strong><\/p>\n\n\n\n<p>Once you have created your own custom view you then need to tell PowerShell to apply the formatting by using the cmdlet <strong>Update-FormatData<\/strong>:<\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\"> \nUpdate-FormatData -PrependPath \"Path To Your XML File\"\n<\/pre>\n\n\n\n<p>Once you run the above command, this custom format you created should now be loaded into memory. You can verify this by using the <strong>Get-FormatData<\/strong> cmdlet:<\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\">Get-FormatData\n<\/pre>\n\n\n\n<p>If you have not done so in your function or script, you can attach your custom view to your function or script using the \u201cinsert\u201d method:<\/p>\n\n\n\n<p class=\"has-text-color has-small-font-size has-purple-color\"><strong>$MyObject.PSObject.TypeNames.Insert(0,\u2019hrmctoolcustomformat\u2019) <\/strong><\/p>\n\n\n\n<p class=\"has-text-color has-small-font-size has-medium-pink-color\"><strong>Note*: This code has already been inserted into our function listed in this example<\/strong>.<br><strong>NOTE**:The first parameter \u201c0\u201d 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:<\/strong><\/p>\n\n\n<pre class=\"lang:PowerShell nums:False\">$MyObject | Get-Member\n<\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<p>A \u201c<em><strong>Typename<\/strong><\/em>\u201d 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 <em><strong>typename<\/strong><\/em> value). This could affect other cmdlets and functions in your script, so be sure to debug if necessary.<\/p>\n\n\n\n<h3 class=\"has-text-align-center wp-block-heading\">HAPPY SCRIPTING! <br>MORE TO COME!!<\/h3>\n\n\n\n<p class=\"has-small-font-size\"><strong>REFERENCES:<\/strong><br><em><a href=\"https:\/\/codingbee.net\/powershell\/powershell-creating-custom-format-views\" target=\"_blank\" rel=\"noreferrer noopener\">Creating Custom Format Views<\/a><\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been doing some training on PowerShell Scripting this week and am going to be posting a number of articles on<\/p>\n<p class=\"link-more\"><a class=\"myButt \" href=\"https:\/\/itblog.ldlnet.net\/index.php\/2020\/04\/22\/powershell-how-to-create-a-custom-view-for-your-ps-output-objects\/\">Read More<\/a><\/p>\n","protected":false},"author":1,"featured_media":147,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3,1],"tags":[8,13],"class_list":["post-720","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-general","category-powershell","category-uncategorized","tag-powershell","tag-script","odd"],"_links":{"self":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts\/720","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/comments?post=720"}],"version-history":[{"count":3,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts\/720\/revisions"}],"predecessor-version":[{"id":724,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/posts\/720\/revisions\/724"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/media\/147"}],"wp:attachment":[{"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/media?parent=720"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/categories?post=720"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itblog.ldlnet.net\/index.php\/wp-json\/wp\/v2\/tags?post=720"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}