Cmdlet
Close-OfficeExcel
Closes a live Excel workbook without saving pending changes.
Syntax
Close-OfficeExcel
[-Document] <ExcelDocument>
[<CommonParameters>]#Parameter set:
DefaultDescription
Use Save-OfficeExcel before closing to retain changes. Workbooks with SaveOnDispose are rejected so disposal cannot publish a file.
Examples
Save edits explicitly, then close.
$workbook | Save-OfficeExcel
$workbook | Close-OfficeExcelClose a workbook without saving
$book = Open-OfficeExcel -Path .\Reports\Q3-Sales.xlsx -ReadOnly
Close-OfficeExcel -Document $bookReleases a workbook that was only read; nothing is written to disk.
Always close after reading
$book = Open-OfficeExcel -Path .\Finance\Purchase-Requests.xlsx -ReadOnly
try {
Import-OfficeExcel -Document $book -Table Requests | Where-Object Status -eq 'Awaiting approval'
} finally {
Close-OfficeExcel -Document $book
}Reads the open purchase requests and closes the workbook in a finally block, so the file is released even if the filter fails.
Parameters
- -Document ExcelDocument
- Explicit-persistence workbook to close.
Common Parameters
This command supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable.
For more information, see about_CommonParameters.