I am trying to save a CSV file using PowerShell, but I am unable to do so. I have tried various ways and spent a lots of hours but still I can't figure out why I am not able to save the resultant file. Below is the sample code for the same.
$file1 = "C:\Users\vicky\desktop\SourceFile.csv" # CSV Source file
$xl = new-object -c excel.application
$xl.Visible = $true
$xl.DisplayAlerts = $false
$wb=$xl.workbooks.open($file1)
$ws=$wb.worksheets.item("SourceFile") # Sheet name
$ws.activate
$rng = $ws.Cells.Item(1,6).EntireColumn
$rng.select #selecting range
$filterval = "Y"
$xl.selection.AutoFilter(9,$filterval) # Applying filter on 9th column from left of the source file
$ws.SaveAs("C:\Users\vicky\Desktop\newFile.csv") # Save filtered output as newFile
$wb.close()
$xl.quit()
$a= get-process -name EXCEL | select -expand Id # Finding Process Id of Excel
stop-process $a # To kill Excel Process
I need to apply filter on a column and then store the filtered file with a new file name. I am able to apply filter but can't save the output to another CSV file.