Submitted By: Kent Finkle

Copies specified columns from a coma-separated values file to an Excel spreadsheet.

PowerShell
Edit|Remove
$comments = @'
Script name: Copy-CsvColumns.ps1
Created on: Saturday, June 23, 2007
Author: Kent Finkle
Purpose: How can I use Windows Powershell to
Copy Selected Columns From a CSV File to an Excel File?
'@
#-----------------------------------------------------
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject(
[System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers() 
}
#-----------------------------------------------------
$xl = new-object -comobject excel.application
$xl.Visible = $True
$wb = $xl.Workbooks.Add()
$ws = $wb.Worksheets.Item(1)
$i = 1
$data = get-content "C:\Scripts\Test.txt" | foreach {
    $ws.Cells.Item($i, 1),$ws.Cells.Item($i, 2) = $_.split(",")
    $i++
}
$a = Release-Ref($ws)
$a = Release-Ref($wb)
$a = Release-Ref($xl)