Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Close specific Excel file using Powershell

Tags:

powershell

What would be the appropriate PowerShell cmdlet to use to kill a specific Excel file. The command

Stop-Process -Name "excel"

closes all open Excel applications.

Thanks in advance!

like image 945
drdrdr Avatar asked Jan 25 '26 15:01

drdrdr


1 Answers

Actually, excel opens .xlsx files in one instance of excel.exe

Kill the excel.exe will kill all opening workbooks. The MainWindowTitle only shows the active workbook, so you will not able to match passive workbooks by the property.

A better way to close a specific workbook is to acquire the excel instance by com object.

Close the workbook matched the Name (file name) or FullName (file path)

$excel = [System.Runtime.InteropServices.Marshal]::GetActiveObject("Excel.Application")
$excel.DisplayAlerts = $false
$excel.Workbooks | %{if($_.Name -imatch "excel file name"){$_.Close()}}
like image 89
Larry Song Avatar answered Jan 28 '26 06:01

Larry Song



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!