Powershell COM Object Disposal
I run into a strange issue with one of my Powershell scripts the other day, I was using an Excel COM object and I had an Excel process even after calling the objects Quit() function.
All the script was doing was to open an Excel xlsx file and save it as csv file. I could use the Get-Process and Stop-Process cmdlets be I would like to be able to schedule the script to run and not be afraid that it would kill any other Excel processes running at the same time.
After a few searches I found out that I could release the COM object with the following command:
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Here we are releasing the COM object excel using the Interopservices. Before doing this, make sure that you have saved your work done using the COM object.
All the script was doing was to open an Excel xlsx file and save it as csv file. I could use the Get-Process and Stop-Process cmdlets be I would like to be able to schedule the script to run and not be afraid that it would kill any other Excel processes running at the same time.
After a few searches I found out that I could release the COM object with the following command:
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($excel)
Here we are releasing the COM object excel using the Interopservices. Before doing this, make sure that you have saved your work done using the COM object.