# Copy-OfficeExcelSheet

Copies selected worksheets within a workbook or from another workbook.

Cmdlet 

**Module**

`PSWriteOffice`

**Inputs**

`OfficeIMO.Excel.ExcelDocument` `System.String[]`

**Outputs**

`OfficeIMO.Excel.ExcelSheet`

**Source**

[CopyOfficeExcelSheetCommand.cs:35](https://github.com/EvotecIT/OfficeIMO/blob/be6c300e4ab1859837b961c64fbfb2b2a1886cd0/PowerShell/PSWriteOffice/Sources/PSWriteOffice/Cmdlets/Excel/CopyOfficeExcelSheetCommand.cs#L35) 

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>]
```

[#](#method-copy-officeexcelsheet-excelworksheetcopymode-exceldocument-string-string-switchparameter-officesheetselector-object-excelsheetnamevalidationmode-switchparameter-switchparameter)

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>]
```

[#](#method-copy-officeexcelsheet-excelworksheetcopymode-string-string-switchparameter-string-officesheetselector-object-excelsheetnamevalidationmode-switchparameter-switchparameter)

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>]
```

[#](#method-copy-officeexcelsheet-excelworksheetcopymode-string-string-string-switchparameter-officesheetselector-object-excelsheetnamevalidationmode-switchparameter-switchparameter)

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.

## Guides & Samples

Authored walkthroughs and practical samples linked to this command.

- [Merge and Compare Excel Workbooks](/docs/pswriteoffice/excel-merge-compare/) Guide
  
  Consolidate selected worksheets and produce explicit workbook or range differences for review and delivery gates.

## Examples

### Copy a template sheet inside a live workbook.

```powershell
Copy-OfficeExcelSheet -Document $book -Sheet Template -Name October
```

### Import regional sheets into an existing workbook.

```powershell
Copy-OfficeExcelSheet -LiteralPath ./Combined.xlsx -Source ./Region.xlsx -Sheet 'Sales*' -NamePrefix 'Region '
```

### Duplicate a sheet

```powershell
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

```powershell
$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](https://learn.microsoft.com/powershell/module/microsoft.powershell.core/about/about_commonparameters).

<!-- PowerForge markdown artifact sha256:F224F0C2E264584DC949DB8450951AED376558109D82C27F6B9FDBFB080AAE6B -->
