How to open excel workbook from powershell for automation How to open excel workbook from powershell for automation powershell powershell

How to open excel workbook from powershell for automation


You need to open it as a ComObject.

$Excel = New-Object -ComObject Excel.Application$Workbook = $Excel.Workbooks.Open($FilePath)

In that example you would have needed to define $FilePath as the full path to the Excel file that you are trying to open.


I've found a nice snippet which also runs a macro here

# start Excel$excel = New-Object -comobject Excel.Application#open file$FilePath = 'C:\temp\Book1.xlsm'$workbook = $excel.Workbooks.Open($FilePath)#make it visible (just to check what is happening)$excel.Visible = $true#access the Application object and run a macro$app = $excel.Application$app.Run("Macro1")


If you just add the path to the variable $FilePath without quotes, it will open automatically:

$FilePath = C:\temp\tempfile.csv

The only problem is that if you did not properly closed the file on Excel, the next time you run it, it will open as READ ONLY and you have to kill the process on TASK manager.

Depending of the needs and coding, sometimes simple works the best.