powershellcomcom-interop

Are there significant differences between using COM directly or using an interop in PowerShell?


I am automating the creation of Excel workbook with PowerShell scripts.

I can create an Excel instance with either

add-type -assembly Microsoft.Office.Interop.Excel
$xls = new-object Microsoft.Office.Interop.Excel.ApplicationClass

or

$xls = new-object -com application.excel

I am wondering if one method is preferred over the other and/or in which uses cases I should go with the former or latter variant.


Solution

  • tl;dr:


    Background information


    It follows from the above:


    As for why you might consider use of the PIA:

    If the above limitations do not apply to you, i.e. if your code must only run in Windows PowerShell and only on machines where the PIA is installed, PIA use has the following - limited - advantages:


    [1] Curiously, I get a mix of PIA type names and System.__ComObject in Windows PowerShell with the PIA installed - I'm unclear on the cause; conceivably, there could be a version mismatch between Excel and the PIA on my machine.