{"id":17171,"date":"2010-09-09T00:01:00","date_gmt":"2010-09-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2010\/09\/09\/copy-csv-columns-to-an-excel-spreadsheet-by-using-powershell\/"},"modified":"2010-09-09T00:01:00","modified_gmt":"2010-09-09T00:01:00","slug":"copy-csv-columns-to-an-excel-spreadsheet-by-using-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/copy-csv-columns-to-an-excel-spreadsheet-by-using-powershell\/","title":{"rendered":"Copy CSV Columns to an Excel Spreadsheet by Using PowerShell"},"content":{"rendered":"<p><strong>Summary<\/strong>: Join the Microsoft Scripting Guys as they show you how to copy CSV columns to a Microsoft Excel spreadsheet by using Windows PowerShell.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/><\/p>\n<p> Hey, Scripting Guy! I love comma-separated value (CSV) files. To me, it seems as if that was the best way to store data. It is almost universally accepted and is still widely used. The files are small and simple to manipulate. I have enjoyed using Windows PowerShell to do text manipulation. And working with a CSV file is, at its core, simple text manipulation. <\/p>\n<p>One reason I like CSV files is that I can easily work with them on server products. And I do not have to eat up several gigabytes worth of space to install Microsoft Office, which is not supported on a server operating system anyway. However, for all its grace and elegance, I will admit that reading a CSV file with the human eye is not the easiest process. For that I prefer to use Microsoft Excel. Though it is true that I can open a CSV file in Microsoft Excel, it is rather annoying when it comes to saving the file. There must be a dozen warnings and prompts I have to navigate. When I have to work on a large number of CSV files, this process becomes loathsome. I would love to be able to copy columns from a CSV file and save the data into an honest to goodness Microsoft Excel spreadsheet and avoid all the prompts. Can this be done?<\/p>\n<p>&#8212; JM<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/> Hello JM, <\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. Well, for the first time in more than four months, I have the windows open and the air conditioner turned off. The leaves have not started changing colors yet, and the temperature at night is only mid 60s Fahrenheit (18 degrees Celsius), but it is a giant step in the right direction. Soon, American football will begin, and there will be another sport I can ignore while I sit quietly beside the fireplace and read Shakespeare. <\/p>\n<p>JM, email that is sent to <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> is not something I can ignore. I wrote the Import-ProcessDataToExcel.ps1 script to illustrate importing specific columns of data into a newly created Microsoft Excel spreadsheet. The complete Import-ProcessDataToExcel.ps1 script is shown here.<\/p>\n<blockquote>\n<p><strong>Import-ProcessDataToExcel.ps1<\/strong><\/p>\n<p><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">       <br \/>&nbsp; <\/span><span style=\"color: #2b91af\">$csvFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;C:\\fso\\process.csv&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">       <br \/>&nbsp; <\/span><span style=\"color: #2b91af\">$path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;C:\\fso\\process.xlsx&#8221;       <br \/><\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #2b91af\">$processes<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Import-Csv<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$csvFile<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #2b91af\">$Excel<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ComObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">excel.application<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #2b91af\">$Excel<\/span><span style=\"color: #000000\">.visible<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$false<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #2b91af\">$workbook<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$Excel<\/span><span style=\"color: #000000\">.workbooks.add()<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;name&#8221;       <br \/><\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;Virtual Memory&#8221;       <br \/><\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">3<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;Working Set&#8221;       <br \/><\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">4<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;Private Memory&#8221;       <br \/><\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">5<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;Non-paged memory&#8221;       <br \/><\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$processes<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #000000\">{<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$process<\/span><span style=\"color: #000000\">.name<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$process<\/span><span style=\"color: #000000\">.vm<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">3<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$process<\/span><span style=\"color: #000000\">.ws<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">4<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$process<\/span><span style=\"color: #000000\">.pm<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">5<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$process<\/span><span style=\"color: #000000\">.npm<\/span><span style=\"color: #808080\">       <br \/>&nbsp;<\/span><span style=\"color: #2b91af\">$i++<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #000000\">}<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">#<\/span><span style=\"color: #000000\">end<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">process<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #2b91af\">$workbook<\/span><span style=\"color: #000000\">.saveas(<\/span><span style=\"color: #2b91af\">$path<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #2b91af\">$Excel<\/span><span style=\"color: #000000\">.Quit()<span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #0000ff\">Remove-Variable<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Name<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">excel<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #0000ff\">gc<\/span><span style=\"color: #000000\">]::collect()<\/span><span style=\"color: #808080\">       <br \/><\/span><span style=\"color: #000000\">[<\/span><span style=\"color: #0000ff\">gc<\/span><span style=\"color: #000000\">]::WaitForPendingFinalizers()<\/span> <\/p>\n<\/blockquote>\n<p>To create an interesting CSV file to work with for today&rsquo;s Hey, Scripting Guy! Blog post, I used Windows PowerShell and I exported process information. I used the <strong>Get-Process<\/strong> cmdlet to retrieve information about all the processes running on my computer. I then piped the object to the <strong>Export-Csv<\/strong> cmdlet. If you are not planning on reconstituting the object, but instead you want to create a traditional CSV file, it is important to include the <strong>notypeinformation<\/strong> switch. Without using this switch, <strong>type<\/strong> information is written to the first line of the CSV file, and it will confuse things. An example of <strong>type<\/strong> information is shown here:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">#TYPE<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">System.Diagnostics.Process<\/span> <\/div>\n<\/blockquote>\n<p>Here is the command I used:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">Get-Process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">|<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Export-Csv<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\fso\\process.csv<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-NoTypeInformation<\/span> <\/div>\n<\/blockquote>\n<p>The resultant CSV file is shown in the following image. JM, I think you were being kind when you said reading the file is not the easiest. To me it looks like a <a href=\"http:\/\/en.wikipedia.org\/wiki\/Rorschach_inkblot_test\">Rorschach test<\/a>. I think I see a Scripting Guy eating an <a href=\"http:\/\/en.wikipedia.org\/wiki\/Anzac_biscuit\">ANZAC biscuit<\/a> while drinking a cup of <a href=\"http:\/\/en.wikipedia.org\/wiki\/Earl_Grey_tea\">Earl Grey tea<\/a>. I wonder what that means? Oh, yeah, it must be time for my afternoon snack. <\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2055.HSG09091001_71EB0EE6.jpg\"><img decoding=\"async\" height=\"387\" width=\"554\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5504.HSG09091001_thumb_5ADC8DAA.jpg\" alt=\"Image of CSV file\" border=\"0\" title=\"Image of CSV file\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" \/><\/a> <\/p>\n<p>The Import-ProcessDataToExcel.ps1 script begins by creating two input parameters. The first parameter is the path to the saved CSV file, and the second parameter is the path for the newly created Microsoft Excel spreadsheet. This is shown here:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #000000\">Param(<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp; <\/span><span style=\"color: #2b91af\">$csvFile<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;C:\\fso\\process.csv&#8221;<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #808080\">       <\/p>\n<p>&nbsp; <\/span><span style=\"color: #2b91af\">$path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;C:\\fso\\process.xlsx&#8221;       <\/p>\n<p><\/span><span style=\"color: #000000\">)<\/span> <\/div>\n<p>&nbsp;<\/p>\n<\/blockquote>\n<p>One thing to keep in mind about this script&mdash;I do not do a check for an existing spreadsheet with the same name in the same location. To do this, you could use the <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/bb978701.aspx\">Test-Path<\/a> cmdlet to see if the file exists, and if it does not exist, use the <strong>saveas<\/strong> method. Otherwise, you might delete the file and then call <strong>saveas<\/strong>. Alternatively, you might want to update the file by using the <strong>save<\/strong> method. As you can tell, there are a number of decisions you might need to make depending on the behavior you want. If the file does exist and the <strong>saveas<\/strong> method is called, you will be prompted to overwrite the file. <\/p>\n<p>The Import-ProcessDataToExcel.ps1 script uses the <strong>Import-Csv<\/strong> cmdlet to import the data from the CSV file. The imported data is stored in the <strong>$processes<\/strong> variable. This is shown here:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$processes<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">Import-Csv<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-Path<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$csvFile<\/span> <\/div>\n<\/blockquote>\n<p>Nearly all of the time when you are working with Microsoft Excel, you will need to create an instance of the <strong>excel.application<\/strong> object. There are exceptions, such as <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2008\/09\/11\/how-can-i-read-from-excel-without-using-excel.aspx\">Hey, Scripting Guy! How Can I Read from Excel Without Using Excel?<\/a> article or <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2008\/09\/15\/how-can-i-write-to-excel-without-using-excel.aspx\">Hey, Scripting Guy! How Can I Write to Excel Without Using Excel?<\/a>, but those are rare cases. After the <strong>application<\/strong> object has been created, set the <strong>visible<\/strong> property to <strong>false<\/strong>, and add a new workbook to the workbooks collection. This is shown here:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$Excel<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">New-Object<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">-ComObject<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">excel.application<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$Excel<\/span><span style=\"color: #000000\">.visible<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$false<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$workbook<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$Excel<\/span><span style=\"color: #000000\">.workbooks.add()<\/span> <\/div>\n<p>&nbsp;<\/p>\n<\/blockquote>\n<p>I then decide to add column headings to the first row. To do this, I use the <strong>cells<\/strong> collection from the <strong>application<\/strong> object. This is a bit of an unusual move because usually the <strong>cells<\/strong> collection is accessed from the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb259441.aspx\">worksheet object<\/a>. The <strong>cells<\/strong> collection from the <strong>application<\/strong> object returns a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/bb178282.aspx\">range object<\/a> that represents all the cells in the active worksheet. The active worksheet in our example is sheet1 in the newly added workbook. By accessing the <strong>cells<\/strong> collection from the <strong>application<\/strong> object, we avoid having to create a <strong>worksheet<\/strong> object. There is a risk in this technique in that if the active document is not a worksheet, the <strong>cells<\/strong> property call will fail, which would cause the entire script to not work. But in this example, there is little danger because as the script is written, the active document must be a worksheet. The first number used by the <strong>item<\/strong> method represents the row, and the second number represents the column. Here are the column heads:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;name&#8221;       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;Virtual Memory&#8221;       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">3<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;Working Set&#8221;       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">4<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;Private Memory&#8221;       <\/p>\n<p><\/span><span style=\"color: #2b91af\">$excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">5<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\"> &#8220;Non-paged memory&#8221;<\/span> <\/div>\n<p>&nbsp;<\/p>\n<\/blockquote>\n<p>We are fished with the first row; therefore, the value of the <strong>$i<\/strong> variable (that will be used later to keep track of our row number) is set to 2. The <strong>foreach<\/strong> statement is used to walk through the collection of processes that are stored in the <strong>$processes<\/strong> variable. This portion of the script is shown here:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #0000ff\">foreach<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #2b91af\">$process<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">in<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$processes<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #000000\">{<\/span> <\/div>\n<p>&nbsp;<\/p>\n<\/blockquote>\n<p>The nice thing about working with CSV data that has been imported via the <strong>Import-Csv<\/strong> cmdlet is that it allows you to address the data by column name. This means the script is easier to read. To add data to the Microsoft Excel spreadsheet, use the <strong>item<\/strong> method from the <strong>cells<\/strong> collection, specify the location on the spreadsheet, and assign the data. By using the <strong>$i<\/strong> variable to keep track of the current row and assigning the other static columns, the following code is achieved:<\/p>\n<blockquote>\n<p><span style=\"color: #2b91af\"> $excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$process<\/span><span style=\"color: #000000\">.name<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #2b91af\"> $excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$process<\/span><span style=\"color: #000000\">.vm<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #2b91af\"> $excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">3<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$process<\/span><span style=\"color: #000000\">.ws<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #2b91af\"> $excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">4<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$process<\/span><span style=\"color: #000000\">.pm<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #2b91af\"> $excel<\/span><span style=\"color: #000000\">.cells.item(<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #000000\">,<\/span><span style=\"color: #800000\">5<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$process<\/span><span style=\"color: #000000\">.npm<\/span> <\/p>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<p>The <strong>++<\/strong> operator is used to increment the value of the <strong>$i<\/strong> variable by 1. The first time through the loop, the value of <strong>$i<\/strong> is equal to 2 because it was set prior to entering the <strong>foreach<\/strong> loop. The next time through, the value will be 3 and so on. The <strong>++<\/strong> operator works the same as saying <strong>$i = $i + 1<\/strong>. This is shown here:<\/p>\n<blockquote>\n<div class=\"code\"><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$i++<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$i<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #800000\">3<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$a<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">2<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$a<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$a<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">+<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #800000\">1<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #2b91af\">$a<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #800000\">3<\/span><span style=\"color: #808080\">       <\/p>\n<p><\/span><span style=\"color: #0000ff\">PS<\/span><span style=\"color: #808080\">&nbsp;<\/span><span style=\"color: #000000\">C:\\&gt;<\/span> <\/div>\n<p>&nbsp;<\/p>\n<\/blockquote>\n<p>The <strong>saveas<\/strong> method from the workbook object automatically creates the file if it does not already exist. If the file does exist, it prompts to overwrite the file. After the file is saved, the <strong>quit<\/strong> method from the <strong>application<\/strong> object is used to exit Microsoft Excel. The <strong>$excel<\/strong> variable is then deleted, and the garbage collection is forced by calling the <a href=\"http:\/\/msdn.microsoft.com\/en-US\/library\/system.gc.collect(v=VS.80).aspx\">collect method<\/a> from the <strong>GC<\/strong> class. The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.gc.waitforpendingfinalizers(v=VS.80).aspx\">WaitForPendingFinalizers Method<\/a> causes the script to wait for all finalizers to complete before continuing. <\/p>\n<p>The completed Microsoft Excel workbook is shown in the following image.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/8231.HSG09091002_0EA483FC.jpg\"><img decoding=\"async\" height=\"373\" width=\"554\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7142.HSG09091002_thumb_06ACE19A.jpg\" alt=\"Image of completed Excel workbook\" border=\"0\" title=\"Image of completed Excel workbook\" style=\"border-bottom: 0px;border-left: 0px;border-top: 0px;border-right: 0px\" \/><\/a> <\/p>\n<p>JM, that is all there is to using Windows PowerShell to create a Microsoft Excel workbook from previously existing CSV data. This also concludes Microsoft Excel Week. Join us tomorrow for <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/quick_2d00_hits+friday\/\">Quick-Hits Friday<\/a>. <\/p>\n<p>We invite you to follow us on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to us at <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p>&nbsp;<\/p>\n<p><strong>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Join the Microsoft Scripting Guys as they show you how to copy CSV columns to a Microsoft Excel spreadsheet by using Windows PowerShell. &nbsp; Hey, Scripting Guy! I love comma-separated value (CSV) files. To me, it seems as if that was the best way to store data. It is almost universally accepted and is [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[48,49,3,45],"class_list":["post-17171","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Join the Microsoft Scripting Guys as they show you how to copy CSV columns to a Microsoft Excel spreadsheet by using Windows PowerShell. &nbsp; Hey, Scripting Guy! I love comma-separated value (CSV) files. To me, it seems as if that was the best way to store data. It is almost universally accepted and is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17171","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=17171"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/17171\/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=17171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=17171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=17171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}