Copy-OfficeExcelSheet
Copies selected worksheets within a workbook or from another workbook.
Syntax
Copy-OfficeExcelSheet
[-CopyMode <Values| Package>]
[-Document <ExcelDocument>]
[[-Name] <String>]
[-NamePrefix <String>]
[-PassThru]
[[-Sheet] <OfficeSheetSelector[]>]
[-Source <Object>]
[-ValidationMode <Sanitize| Strict>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]#DocumentCopy-OfficeExcelSheet
[-CopyMode <Values| Package>]
[[-Name] <String>]
[-NamePrefix <String>]
[-PassThru]
[-Path] <String[]>
[[-Sheet] <OfficeSheetSelector[]>]
[-Source <Object>]
[-ValidationMode <Sanitize| Strict>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]#PathCopy-OfficeExcelSheet
[-CopyMode <Values| Package>]
-LiteralPath <String[]>
[[-Name] <String>]
[-NamePrefix <String>]
[-PassThru]
[[-Sheet] <OfficeSheetSelector[]>]
[-Source <Object>]
[-ValidationMode <Sanitize| Strict>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]#LiteralPathDescription
Source accepts a native ExcelDocument, a workbook path or literal FileInfo. Sheet accepts names, wildcard patterns, one-based positions and native worksheets. Omit Sheet to copy all source sheets, or the current worksheet inside a worksheet content block. Native package copy preserves references between selected sheets. File targets are saved in place; live documents require an explicit save.
PassThru emits native worksheets. For file input, their workbook remains open: close the returned worksheet's Document with Close-OfficeExcel when finished.
Examples
Copy a template sheet inside a live workbook.
Copy-OfficeExcelSheet -Document $book -Sheet Template -Name OctoberImport regional sheets into an existing workbook.
Copy-OfficeExcelSheet -LiteralPath ./Combined.xlsx -Source ./Region.xlsx -Sheet 'Sales*' -NamePrefix 'Region 'Duplicate a sheet
Copy-OfficeExcelSheet -Path .\Reports\Timesheets.xlsx -Sheet October -Name NovemberCopies the October sheet in the same workbook as a starting point for November.
Collect sheets from several workbooks
$pack = Open-OfficeExcel -Path .\Reports\Board-Pack.xlsx
foreach ($file in Get-ChildItem -Path .\Regions -Filter *.xlsx) {
Copy-OfficeExcelSheet -Document $pack -Source $file.FullName -Sheet Summary -Name "$($file.BaseName) summary"
}
Save-OfficeExcel -Document $pack -PassThru | Close-OfficeExcelCopies the Summary sheet of each regional workbook into the board pack, prefixed with the region name.
Parameters
- -CopyMode ExcelWorksheetCopyMode
- Gets or sets native package copying or explicit value-only materialization.
- Possible values:
Values,Package - -Document ExcelDocument
- Gets or sets a live target workbook; omission uses the enclosing workbook scope.
- -Name String
- Gets or sets a new name when copying exactly one worksheet.
- -NamePrefix String
- Gets or sets a prefix for each copied worksheet name.
- -PassThru SwitchParameter
- Gets or sets whether to return live copied worksheets; their workbook is caller-owned.
- -Sheet OfficeSheetSelector[]
- Gets or sets source worksheet names or patterns, one-based positions or native worksheets.
- -Source Object
- Gets or sets another workbook as a native ExcelDocument, path or literal FileInfo.
- -ValidationMode ExcelSheetNameValidationMode
- Gets or sets how invalid or duplicate destination names are handled.
- Possible values:
Sanitize,Strict - -WhatIf SwitchParameter
- Shows what would happen if the command runs.
- -Confirm SwitchParameter
- Prompts for confirmation before running the command.
- -Path String[]
- Gets or sets target workbook paths with filesystem wildcard expansion.
- -LiteralPath String[]
- Gets or sets literal target paths, including FileInfo.FullName and provider PSPath.
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.