Skip to main content

Search OfficeIMO

Enter a topic, API type, or PowerShell command.

Documentation Search all OfficeIMO/
Cmdlet

New-OfficeExcel

Module
PSWriteOffice
Inputs
None
Outputs
OfficeIMO.Excel.ExcelDocument System.IO.FileInfo

Creates a themed Excel workbook, returning a live workbook or saving a completed content block.

Syntax

New-OfficeExcel
[-DateSystem <NineteenHundred|NineteenFour>]
[-DocumentProperty <Hashtable>]
[-Force]
[-MaxDegreeOfParallelism <Int32>]
[[-Path] <String>]
[-Template <String>]
[-Theme <OfficeThemeInput>]
[<CommonParameters>]
#
Parameter set: Live
New-OfficeExcel
[-AllowLoss]
[-Content] <ScriptBlock>
[-DateSystem <NineteenHundred|NineteenFour>]
[-DocumentProperty <Hashtable>]
[-Force]
[-MaxDegreeOfParallelism <Int32>]
[-PassThru]
[-Password <SecureString>]
[-Path] <String>
[-SaveOptions <ExcelSaveOptions>]
[-Show]
[-Template <String>]
[-Theme <OfficeThemeInput>]
[<CommonParameters>]
#
Parameter set: Content

Description

Without a block no file is written, including on disposal. A block saves atomically; an existing destination requires Force. Template copies content; Theme reads only the visual theme. SaveOptions and Password apply to a completed content block.

Examples

Create a workbook and return its saved file.

New-OfficeExcel -Path ./Report.xlsx -Theme Modern -PassThru {
    ExcelSheet 'Data' { ExcelCell -Address A1 -Value 'Region' }
}

Create a live workbook for explicit editing and saving.

$workbook = New-OfficeExcel -Theme Boardroom
$sheet = Add-OfficeExcelSheet -Document $workbook -Name Data -PassThru
Set-OfficeExcelCell -Sheet $sheet -Address A1 -Value 'Region'
Save-OfficeExcel -Document $workbook -Path ./Report.xlsx
Close-OfficeExcel -Document $workbook

Create a workbook with one table

New-OfficeExcel -Path .\Services.xlsx {
    ExcelSheet 'Services' {
        ExcelTable (Get-Service | Select-Object Name, DisplayName, Status, StartType)
    }
}

Creates Services.xlsx with a themed, filterable table of the local services and saves it when the block ends.

Build a regional sales workbook

$orders = Import-OfficeCsv -Path .\Data\Q3-Orders.csv
$byRegion = $orders | Group-Object Region | ForEach-Object {
    [pscustomobject]@{
        Region  = $_.Name
        Orders  = $_.Count
        Revenue = ($_.Group | Measure-Object Amount -Sum).Sum
    }
}

$properties = @{ Title = 'Q3 sales'; Company = 'Northwind Traders' }
New-OfficeExcel -Path .\Reports\Q3-Sales.xlsx -Theme Boardroom -DocumentProperty $properties -Force -Show {
    ExcelSheet 'Summary' {
        ExcelTable $byRegion -Name RegionSummary
        ExcelChart -Table RegionSummary -Address F2 -Type Column -Title 'Revenue by region'
    }
    ExcelSheet 'Orders' {
        ExcelTable $orders -Name Orders
    }
}

Builds a Q3 sales workbook in the Boardroom theme with a summary chart and the order detail, sets the document title and opens it in Excel.

Keep a workbook open and save it later

$book = New-OfficeExcel -Theme Modern
foreach ($server in 'SQL01', 'SQL02', 'WEB01') {
    $disks = Invoke-Command -ComputerName $server { Get-Volume | Where-Object DriveLetter }
    Add-OfficeExcelSheet -Document $book -Name $server {
        ExcelTable ($disks | Select-Object DriveLetter, FileSystemLabel, Size, SizeRemaining)
    }
}
Save-OfficeExcel -Document $book -Path .\Reports\Disk-Capacity.xlsx
Close-OfficeExcel -Document $book

Without a content block the live workbook is returned, so a script can add a sheet per server in a loop and choose the file name when it saves.

Parameters

-DateSystem ExcelDateSystem optional
Native workbook date system.
Possible values: NineteenHundred, NineteenFour
-DocumentProperty Hashtable optional
Workbook properties such as Title, Author, Subject, Keywords, Company and Manager.
-Force SwitchParameter optional
Allow a completed content block to replace an existing destination.
-MaxDegreeOfParallelism Int32 optional
Maximum parallel workers; 1 runs sequentially. Omission retains the native execution policy.
-Path String required in Contentposition: 0aliases: FilePath
Optional associated destination for a live document; required when a content block saves a file.
-Template String optional
Existing workbook copied as starting content. Its styling is preserved unless Theme is supplied.
-Theme OfficeThemeInput optional
Built-in theme, theme object or a document/template whose visual theme is read.
-AllowLoss SwitchParameter optionalsets: Content
Explicitly accept reductions reported by the content block's native save.
-Content ScriptBlock requiredposition: 1sets: Content
Authoring block to complete before the document is atomically saved and closed.
-PassThru SwitchParameter optionalsets: Content
Return the saved FileInfo after a completed content block.
-Password SecureString optionalsets: Content
Password for a completed content block's saved file; use Save-OfficeExcel for a live workbook.
-SaveOptions ExcelSaveOptions optionalsets: Content
Native save options or a hashtable, applied to a completed content block.
-Show SwitchParameter optionalsets: Content
Open the saved result in its registered application after the content block succeeds.

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.