使用 Powershell 关闭特定 Excel 文件

drd*_*rdr 3 powershell

用于终止特定 Excel 文件的适当 PowerShell cmdlet 是什么?命令

Stop-Process -Name "excel"
Run Code Online (Sandbox Code Playgroud)

关闭所有打开的 Excel 应用程序。

提前致谢!

Lar*_*ong 5

.xlsx实际上,Excel在一个实例中打开文件excel.exe

终止 excel.exe 将终止所有打开的工作簿。仅MainWindowTitle显示活动工作簿,因此您将无法通过属性匹配被动工作簿。

关闭特定工作簿的更好方法是通过 com 对象获取 excel 实例。

Name关闭与(文件名)或FullName(文件路径)匹配的工作簿

$excel = [System.Runtime.InteropServices.Marshal]::GetActiveObject("Excel.Application")
$excel.DisplayAlerts = $false
$excel.Workbooks | %{if($_.Name -imatch "excel file name"){$_.Close()}}
Run Code Online (Sandbox Code Playgroud)