Marleen,
I have included a code segment below that shows how I handle the problem with Excel not closing. What you would be interested in is the first line that get a list of existing Excel processes before the Excel automation starts and the last line that closes any new Excel processes since the snapshot. The segment just opens Excel and saves the file as a .CSV. It assumes that there are no Excel processes started that you would care about in the meantime–a safe assumption in my case.
$runningExcelProcesses = @(Get-Process -Name excel -ErrorAction SilentlyContinue | Select-Object ID)
$runningExcelProcesses
$xls=”T:somefilepath.xlsx”
$csv=”T:someotherfilepath.csv”
# You can use the Excel COM object in PowerShell to save the file as a CSV file. http://www.powershellcommunity.org/Forums/tabid/54/aft/657/Default.aspx
# $xlCSV is a constant for the SaveAs method that tells Excel to save the file in the CSV format.
# xlWorkbookDefault = 51 http://www.excel-answers.com/microsoft/Excel-Programming/34263208/fileformat-list.aspx and http://www.pcreview.co.uk/forums/use-automation-save-xls-into-pdf-office-2007-a-t3042211.html
new-variable xlCSV 6 -option Constant -ErrorAction SilentlyContinue # $xlCSV=6
new-variable xlWorkbookDefault 51 -option Constant -ErrorAction SilentlyContinue # $xlWorkbookDefault=51
$xl=New-Object -com “Excel.Application”
$wb=$xl.workbooks.open($xls)
$wb.SaveAs($csv,$xlCSV) # create a CSV version of the file
$xl.displayalerts=$False
$xl.quit()
# One thing that seems to be a problem though is that even though you quit Excel, the Excel process doesn’t really terminate so you may also need kill it
# if (ps excel) { kill -name excel}
Get-Process -Name excel -ErrorAction SilentlyContinue | Select-Object ID | Where-Object {$runningExcelProcesses.ID -notcontains $_.ID} | Stop-Process
Steve