Save-OfficeExcel
Saves a live Excel workbook without closing it.
Syntax
Save-OfficeExcel
[-AllowLoss]
[-Document] <ExcelDocument>
[-PassThru]
[-Password <SecureString>]
[-Path <String>]
[-SaveOptions <ExcelSaveOptions>]
[-Show]
[<CommonParameters>]#DefaultDescription
Omitted Password preserves loaded encryption; an explicit null removes it. SaveOptions accepts native options or a hashtable. PassThru returns the saved FileInfo. The workbook must use explicit persistence.
Examples
Save pending edits and then close the workbook.
Save-OfficeExcel -Document $workbook -Path ./Reviewed.xlsx -SaveOptions @{ Preflight = $true; ValidateOpenXml = $true } -PassThru
Close-OfficeExcel -Document $workbookSave a live workbook
$book = Open-OfficeExcel -Path .\Reports\Inventory.xlsx
Save-OfficeExcel -Document $book
Close-OfficeExcel -Document $bookSaves a workbook opened without a content block back to its own path and closes it.
Save a validated copy under a new name
$book = New-OfficeExcel
Add-OfficeExcelSheet -Document $book -Name 'Changes' {
ExcelTable (Import-OfficeCsv -Path .\Data\Changes-Week43.csv)
}
$book |
Save-OfficeExcel -Path .\Reports\Changes-Week43.xlsx -SaveOptions @{ ValidateOpenXml = $true } -PassThru
Close-OfficeExcel -Document $bookWrites this week's change log to a new file with Open XML validation, returns the saved file, and closes the live workbook explicitly.
Save with a password
$password = Read-Host -AsSecureString -Prompt 'Workbook password'
$book = Open-OfficeExcel -Path .\Reports\Salaries-2027.xlsx
Save-OfficeExcel -Document $book -Path .\Secure\Salaries-2027.xlsx -Password $password
Close-OfficeExcel -Document $bookEncrypts the saved file with a password read at the prompt, so it never appears in the script or history.
Parameters
- -AllowLoss SwitchParameter
- Explicitly accept reported native-save reductions; overrides SaveOptions when supplied.
- -Document ExcelDocument
- Live workbook to save.
- -PassThru SwitchParameter
- Return the saved FileInfo.
- -Password SecureString
- Override the output password; omission retains loaded encryption.
- -Path String
- Optional new destination; omission uses the associated path.
- -SaveOptions ExcelSaveOptions
- Native save settings, or a hashtable using the save-option factory's names.
- -Show SwitchParameter
- Open the saved file in its registered default application.
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.