{"id":4481,"date":"2012-12-11T00:01:00","date_gmt":"2012-12-11T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2012\/12\/11\/use-powershell-to-add-two-pieces-of-csv-data-together\/"},"modified":"2012-12-11T00:01:00","modified_gmt":"2012-12-11T00:01:00","slug":"use-powershell-to-add-two-pieces-of-csv-data-together","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-add-two-pieces-of-csv-data-together\/","title":{"rendered":"Use PowerShell to Add Two Pieces of CSV Data Together"},"content":{"rendered":"<p><strong>Summary:<\/strong> Microsoft Scripting Guy, Ed Wilson, talks about adding together two pieces of CSV information and making the rows wider, not longer.\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\"> Hey, Scripting Guy! I have a problem at work. Our company is in the midst of massive acquisitions and mergers. I mean every few weeks our network completely changes, and we have to absorb a new domain or forest into our central Active Directory infrastructure. To make matters worse, we are getting ready for a SOX audit. I mean, like really. Anyway, what I have decided to do is to store certain information into a SQL Express database. The problem, of course, is that I do not know very much about SQL Server. I want to import CSV data into the database. I know how to do that (I do read the Hey, Scripting Guy! blog every day). What I do not know is how to take two WMI classes and turn them into a single line of properties and a single line of data in CSV format. Can you help me?\n&mdash;TU\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 TU,\nMicrosoft Scripting Guy, Ed Wilson, is here. Well, today, it is sort of soggy outside. It is damp, overcast, and drizzling rain. I figured it would be a good chance to have a &ldquo;<a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2009\/03\/04\/how-do-i-migrate-my-vbscript-wmi-queries-to-windows-powershell.aspx\" target=\"_blank\">coffee day<\/a>,&rdquo; so I got up early, ground some Kona beans, boiled fresh spring water, and dumped the contents into my French press. While the coffee and the hot spring water became acquainted, I gently warmed some organic cream by placing the cream cup in a bowl of the hot water. I also took the opportunity to &ldquo;prime&rdquo; my ceramic Scripting Guys mug by pouring the remaining hot water into it. I then snatched the Scripting Wife&rsquo;s Surface (she is out with friends perusing holiday specials) and decided to catch up on some of the Scripter email.\nTU, I have <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/csv+and+other+delimited+files\/windows+powershell\/\" target=\"_blank\">written extensively about working with CSV files<\/a>, but I do not think anyone has asked about making the columns wider. Usually, when appending one CSV file to another, the process involves adding additional rows of the same sort of data.<\/p>\n<h2>Adding one CSV to another one<\/h2>\n<p>As an example, I am going to query the <strong>Win32_ComputerSystem WMI<\/strong> class and the <strong>Win32_OperatingSystem WMI<\/strong> class. The first thing I do is initialize all variables by setting their value to <strong>$null<\/strong>. This keeps me from getting confused when I run the script more than one time&mdash;it ensures that the script starts clean each time it runs. Here is the line of code I use.<\/p>\n<p style=\"padding-left: 30px\">$computer = $osInfo = $compOSInfo = $null\nI am only interested in a few properties from each WMI class. For compatibility reasons, I am using the <strong>Get-WmiObject<\/strong> Windows PowerShell cmdlet (although the technique works just as well with the <strong>Get-CimInstance<\/strong> cmdlet). I use the <strong>Select-Object<\/strong> cmdlet to choose specific properties from each WMI class, and then I use the <strong>Convertto-CSV<\/strong> cmdlet to convert the selected information into CSV format. I do not need the type information because I have no intentions of reconstituting the types, and, therefore, I use the <strong>&ndash;NoTypeInformation<\/strong> switched parameter. Here are the two queries.<\/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\">&nbsp;&nbsp;&nbsp; ConvertTo-Csv -NoTypeInformation&nbsp;&nbsp;<\/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\">&nbsp;&nbsp;&nbsp; ConvertTo-Csv -NoTypeInformation<\/p>\n<h2>Create a new column header and a new row of data<\/h2>\n<p>Now I need to add the two column headers together. The <strong>$computer<\/strong> variable contains two rows of information. The first row is the column headings, and the second row contains the data associated with the column headings. The contents of the <strong>$computer<\/strong> variable are shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $computer<\/p>\n<p style=\"padding-left: 30px\">&#8220;numberOfProcessors&#8221;,&#8221;NumberOfLogicalProcessors&#8221;,&#8221;HypervisorPresent&#8221;<\/p>\n<p style=\"padding-left: 30px\">&#8220;1&#8221;,&#8221;8&#8243;,&#8221;True&#8221;\n&nbsp;The <strong>$osInfo<\/strong> variable contains a different assortment of column headings and associated data. This is shown here.<\/p>\n<p style=\"padding-left: 30px\">PS C:&gt; $osInfo<\/p>\n<p style=\"padding-left: 30px\">&#8220;version&#8221;,&#8221;caption&#8221;,&#8221;serialnumber&#8221;,&#8221;osarchitecture&#8221;<\/p>\n<p style=\"padding-left: 30px\">&#8220;6.2.9200&#8221;,&#8221;Microsoft Windows 8 Pro&#8221;,&#8221;00178-10777-21922-AA381&#8243;,&#8221;64-bit&#8221;\nTo create a new column header row, I need to add the first line from each of the <strong>$computer<\/strong> and the <strong>$osInfo<\/strong> variables. I also need to ensure I have a comma between the two first rows. I store the results in a new variable I call <strong>$compOSInfo<\/strong>. Here is the code.<\/p>\n<p style=\"padding-left: 30px\">$compOSInfo = &#8220;{0},{1}&#8221; -f $computer[0],$osInfo[0]\nNow I need to create the associated data row for the new heading row. To do this, I will need to ensure that I begin on the second line. To do this, I use the <strong>&ldquo;`r`n&rdquo;<\/strong> special symbol. Once I am on the new row, I add the contents of the second line of the <strong>$computer<\/strong> variable with the contents of the second line of the <strong>$osInfo<\/strong> variable. Once again, I make sure I have a comma to separate the two blocks of information. The following line of code accomplishes this task.<\/p>\n<p style=\"padding-left: 30px\">$compOSInfo += &#8220;`r`n{0},{1}&#8221; -f $computer[1],$osInfo[1]\n&nbsp;The complete Get-OSAndComputerInfo.ps1 script is shown here.\n<strong>Get-OSAndComputerInfo.ps1<\/strong><\/p>\n<p style=\"padding-left: 30px\">$computer = $osInfo = $compOSInfo = $null<\/p>\n<p style=\"padding-left: 30px\">&nbsp;$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\">&nbsp;&nbsp;&nbsp; ConvertTo-Csv -NoTypeInformation&nbsp;&nbsp;<\/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\">&nbsp;&nbsp;&nbsp; ConvertTo-Csv -NoTypeInformation<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">$compOSInfo = &#8220;{0},{1}&#8221; -f $computer[0],$osInfo[0]<\/p>\n<p style=\"padding-left: 30px\">$compOSInfo += &#8220;`r`n{0},{1}&#8221; -f $computer[1],$osInfo[1]<\/p>\n<p style=\"padding-left: 30px\">$compOSInfo\nTU, that is all there is to adding together two CSV pieces of information. Join me tomorrow when I will talk about more cool 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>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about adding together two pieces of CSV information and making the rows wider, not longer. Hey, Scripting Guy! I have a problem at work. Our company is in the midst of massive acquisitions and mergers. I mean every few weeks our network completely changes, and we have to [&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-4481","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, talks about adding together two pieces of CSV information and making the rows wider, not longer. Hey, Scripting Guy! I have a problem at work. Our company is in the midst of massive acquisitions and mergers. I mean every few weeks our network completely changes, and we have to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4481","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=4481"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/4481\/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=4481"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=4481"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=4481"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}