Cmdlet
Add-OfficeExcelSheet
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:
DefaultDescription
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
- Code to execute inside the worksheet context.
- -Document ExcelDocument
- Workbook that will receive the worksheet.
- -Name String
- Name of the worksheet to create or reuse. When omitted the last sheet is reused or a default sheet is created.
- -PassThru SwitchParameter
- Emit the ExcelSheet object after execution.
- -ValidationMode ExcelSheetNameValidationMode
- 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.