{"id":4478,"date":"2012-12-12T00:01:00","date_gmt":"2012-12-12T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/12\/12\/create-a-custom-powershell-object-to-simplify-appending-csv-files\/"},"modified":"2012-12-12T00:01:00","modified_gmt":"2012-12-12T00:01:00","slug":"create-a-custom-powershell-object-to-simplify-appending-csv-files","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/create-a-custom-powershell-object-to-simplify-appending-csv-files\/","title":{"rendered":"Create a Custom PowerShell Object to Simplify Appending CSV files"},"content":{"rendered":"<p><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, shows how to create a custom Windows PowerShell object to simplify appending to CSV files.\n&nbsp;<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;Hey, Scripting Guy! &nbsp;Yesterday, you talked about creating two objects, exporting them to CSV files, and then manipulating the strings to combine the CSV files. Is there a cleaner way of doing this?\n&mdash;CS\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello CS,\nMicrosoft Scripting Guy, Ed Wilson, is here. This morning I am sitting on the lanai and sipping a cup of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Assam_tea\" target=\"_blank\">Assam black tea<\/a>. I brought the leaves back from Munich, Germany (by the way, if you bring back several bags of tea leaves, be prepared for additional customs screening when you return to your home country&mdash;at least that is what happened to me). The tea is a robustly flavored black tea. I added a few organic rose petals to the pot to give it a nice, mellow flavor.\nCS, of course, there are other ways of combining WMI objects and creating CSV formatted output. In Windows PowerShell, there are always multiple ways of accomplishing the same task.<\/p>\n<h2>First create a custom object<\/h2>\n<p><strong>Note<\/strong> &nbsp;&nbsp;Today&rsquo;s post is basically a continuation of yesterday&rsquo;s blog post,&nbsp;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2012\/12\/11\/use-powershell-to-add-two-pieces-of-csv-data-together.aspx\" target=\"_blank\">Use PowerShell to Add Two Pieces of CSV Data Together<\/a><em>.<\/em> It might be a good idea to read that article first.\nThe first step is to create a couple of custom objects. The easiest way to create a custom object is to use the <strong>Select-Object<\/strong> cmdlet. This cmdlet permits easy conversion from one type of object to a custom object. In the custom object, the properties and values appear as <strong>noteproperty<\/strong><em> <\/em>data types, as shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $osInfo = gwmi win32_operatingsystem |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; select version, caption, serialnumber, osarchitecture<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $osInfo | gm -MemberType NoteProperty<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; TypeName: Selected.System.Management.ManagementObject<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Name&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MemberType&nbsp;&nbsp; Definition&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8212;&#8212;&#8212;-&nbsp;&nbsp; &#8212;&#8212;&#8212;-&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">caption&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NoteProperty System.String caption=Microsoft Windows 8 Pro&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">osarchitecture NoteProperty System.String osarchitecture=64-bit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<\/p>\n<p style=\"padding-left: 30px\">serialnumber&nbsp;&nbsp; NoteProperty System.String serialnumber=00178-10777-21922-AA381<\/p>\n<p style=\"padding-left: 30px\">version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; NoteProperty System.String version=6.2.9200&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\nBecause I am already dealing with an object, I can easily extend that object by using the <strong>Add-Member<\/strong> cmdlet. The <strong>Add-Member<\/strong> cmdlet adds additional members to an existing object thereby permitting the extending of existing objects. I have my computer information stored in a custom object in a variable&mdash;just as I also have the operating system information stored in a custom object. The Windows PowerShell code to create the two objects is shown here.<\/p>\n<p style=\"padding-left: 30px\">$computer = gwmi win32_computersystem |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; select numberOfProcessors, NumberOfLogicalProcessors, HypervisorPresent<\/p>\n<p style=\"padding-left: 30px\">$osInfo = gwmi win32_operatingsystem |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; select version, caption, serialnumber, osarchitecture&nbsp;<\/p>\n<h2>Extending an existing object<\/h2>\n<p>If I wanted to add only one or two properties to an existing object, I might do it manually. But because I already have an object&mdash;with an undetermined number of properties&mdash;I want an automatic way to add the members to the object. To do this, I use the <strong>Get-Member<\/strong> cmdlet to find the members of the object I wish to locate and to use. As illustrated earlier, the properties I am interested in obtaining are <strong>NoteProperty<\/strong><em> <\/em>member types. Therefore, I use the <strong>Foreach<\/strong> statement to walk through the members of the custom object stored in the <strong>$osInfo<\/strong> variable&mdash;but only if <strong>-MemberType<\/strong> is a <strong>NoteProperty<\/strong><em> <\/em>data type<em>. <\/em>This line of code is shown here.<\/p>\n<p style=\"padding-left: 30px\">Foreach($p in Get-Member -InputObject $osInfo -MemberType NoteProperty)\nNow, I need to use the <strong>Add-Member<\/strong> cmdlet to add the <strong>NoteProperty<\/strong><em> <\/em>member types to the custom object stored in the <strong>$computer<\/strong> variable. I use the object stored in the <strong>$computer<\/strong> variable for the <strong>InputObject<\/strong><em>, <\/em>and I add the new members to the object. This line of code is shown here (the back tick is line continuation&mdash;I broke the line for readability). The subexpression is required to obtain the value stored in each property.<\/p>\n<p style=\"padding-left: 30px\">Add-Member -InputObject $computer -MemberType NoteProperty `<\/p>\n<p style=\"padding-left: 30px\">&nbsp; -Name $p.Name -Value $osInfo.$($p.Name) -Force\nOnce modified, the new object easily creates CSV output when piped to the <strong>ConvertTo-CSV<\/strong> cmdlet (or to the <strong>Export-CSV<\/strong> cmdlet).\nThe complete Get-OsAndComputerInfoAddMember.ps1 script is shown here.<\/p>\n<p style=\"padding-left: 30px\"><strong>Get-OsAndComputerInfoAddMember.ps1<\/strong><\/p>\n<p style=\"padding-left: 30px\">$computer = $osInfo = $null<\/p>\n<p style=\"padding-left: 30px\">$computer = gwmi win32_computersystem |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; select numberOfProcessors, NumberOfLogicalProcessors, HypervisorPresent<\/p>\n<p style=\"padding-left: 30px\">$osInfo = gwmi win32_operatingsystem |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; select version, caption, serialnumber, osarchitecture<\/p>\n<p style=\"padding-left: 30px\">Foreach($p in Get-Member -InputObject $osInfo -MemberType NoteProperty)<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp;Add-Member -InputObject $computer -MemberType NoteProperty `<\/p>\n<p style=\"padding-left: 30px\">&nbsp; -Name $p.Name -Value $osInfo.$($p.Name) -Force<\/p>\n<p style=\"padding-left: 30px\">}<\/p>\n<p style=\"padding-left: 30px\">$computer | ConvertTo-Csv -NoTypeInformation<\/p>\n<p style=\"padding-left: 30px\">&nbsp;\nCS, that is all there is to using Windows PowerShell to create a custom object and append to CSV data. Join me tomorrow when I will talk about cool Windows PowerShell stuff.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.\n<strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to create a custom Windows PowerShell object to simplify appending to CSV files. &nbsp;&nbsp;Hey, Scripting Guy! &nbsp;Yesterday, you talked about creating two objects, exporting them to CSV files, and then manipulating the strings to combine the CSV files. Is there a cleaner way of doing this? &mdash;CS [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[169,3,4,45],"class_list":["post-4478","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-csv-and-other-delimited-files","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, shows how to create a custom Windows PowerShell object to simplify appending to CSV files. &nbsp;&nbsp;Hey, Scripting Guy! &nbsp;Yesterday, you talked about creating two objects, exporting them to CSV files, and then manipulating the strings to combine the CSV files. Is there a cleaner way of doing this? &mdash;CS [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4478","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=4478"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4478\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=4478"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4478"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4478"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}