Skip to main content

Search OfficeIMO

Enter a topic, API type, or PowerShell command.

Documentation Search all OfficeIMO/
Cmdlet

Add-OfficeExcelSheet

Aliases: ExcelSheet
Module
PSWriteOffice
Aliases
ExcelSheet
Inputs
OfficeIMO.Excel.ExcelDocument
Outputs
OfficeIMO.Excel.ExcelSheet

Adds or reuses a worksheet within the current Excel DSL scope.

Syntax

Add-OfficeExcelSheet
[[-Content] <ScriptBlock>]
[-Document <ExcelDocument>]
[[-Name] <String>]
[-PassThru]
[-ValidationMode <Sanitize|Strict>]
[<CommonParameters>]
#
Parameter set: Default

Description

Creates the sheet when missing, pushes it onto the DSL stack, and executes the nested script block.

Examples

Add a sheet with content

New-OfficeExcel -Path .\Inventory.xlsx {
    ExcelSheet 'Servers' {
        ExcelTable (Import-OfficeCsv -Path .\Data\Servers.csv)
    }
}

Adds an Inventory sheet and writes a table of servers into it.

Create one sheet per department

$staff = Import-OfficeCsv -Path .\HR\Headcount-2026-10.csv
New-OfficeExcel -Path .\Reports\Headcount.xlsx {
    foreach ($group in $staff | Group-Object Department) {
        ExcelSheet $group.Name -ValidationMode Sanitize {
            ExcelTable ($group.Group | Select-Object Name, Title, Location, StartDate)
        }
    }
}

Groups the headcount export by department and writes each group to its own sheet, with a cleaned-up name when a department name has characters Excel does not allow.

Parameters

-Content ScriptBlock optionalposition: 1
Code to execute inside the worksheet context.
-Document ExcelDocument optionalpipeline: True (ByValue)
Workbook that will receive the worksheet.
-Name String optionalposition: 0
Name of the worksheet to create or reuse. When omitted the last sheet is reused or a default sheet is created.
-PassThru SwitchParameter optional
Emit the ExcelSheet object after execution.
-ValidationMode ExcelSheetNameValidationMode optional
Controls how invalid sheet names are handled.
Possible values: Sanitize, Strict

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.