Skip to main content

Search OfficeIMO

Enter a topic, API type, or PowerShell command.

Documentation Search all OfficeIMO/
Cmdlet

Copy-OfficeExcelSheet

Module
PSWriteOffice
Inputs
OfficeIMO.Excel.ExcelDocument System.String[]
Outputs
OfficeIMO.Excel.ExcelSheet

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>]
#
Parameter set: Document
Copy-OfficeExcelSheet
[-CopyMode <Values|Package>]
[[-Name] <String>]
[-NamePrefix <String>]
[-PassThru]
[-Path] <String[]>
[[-Sheet] <OfficeSheetSelector[]>]
[-Source <Object>]
[-ValidationMode <Sanitize|Strict>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
#
Parameter set: Path
Copy-OfficeExcelSheet
[-CopyMode <Values|Package>]
-LiteralPath <String[]>
[[-Name] <String>]
[-NamePrefix <String>]
[-PassThru]
[[-Sheet] <OfficeSheetSelector[]>]
[-Source <Object>]
[-ValidationMode <Sanitize|Strict>]
[-WhatIf]
[-Confirm]
[<CommonParameters>]
#
Parameter set: LiteralPath

Description

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 October

Import 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 November

Copies 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-OfficeExcel

Copies the Summary sheet of each regional workbook into the board pack, prefixed with the region name.

Parameters

-CopyMode ExcelWorksheetCopyMode optional
Gets or sets native package copying or explicit value-only materialization.
Possible values: Values, Package
-Document ExcelDocument optionalpipeline: True (ByValue)sets: Document
Gets or sets a live target workbook; omission uses the enclosing workbook scope.
-Name String optionalposition: 2
Gets or sets a new name when copying exactly one worksheet.
-NamePrefix String optional
Gets or sets a prefix for each copied worksheet name.
-PassThru SwitchParameter optional
Gets or sets whether to return live copied worksheets; their workbook is caller-owned.
-Sheet OfficeSheetSelector[] optionalposition: 1aliases: WorksheetName
Gets or sets source worksheet names or patterns, one-based positions or native worksheets.
-Source Object optional
Gets or sets another workbook as a native ExcelDocument, path or literal FileInfo.
-ValidationMode ExcelSheetNameValidationMode optional
Gets or sets how invalid or duplicate destination names are handled.
Possible values: Sanitize, Strict
-WhatIf SwitchParameter optional
Shows what would happen if the command runs.
-Confirm SwitchParameter optional
Prompts for confirmation before running the command.
-Path String[] requiredposition: 0pipeline: True (ByValue)aliases: FilePathsets: Path
Gets or sets target workbook paths with filesystem wildcard expansion.
-LiteralPath String[] requiredpipeline: True (ByPropertyName)aliases: FullName, PSPathsets: LiteralPath
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.