Cmdlet
Add-OfficeExcelConnection
Adds Power Query or connection metadata that Excel can own and refresh.
Syntax
Add-OfficeExcelConnection
[-CommandText <String>]
[-Description <String>]
[-Document <ExcelDocument>]
-Name <String>
[-PassThru]
[-QueryTable <String>]
[-RefreshOnOpen]
[-Sheet <OfficeSheetSelector>]
[<CommonParameters>]#Parameter set:
PowerQueryAdd-OfficeExcelConnection
[-Document <ExcelDocument>]
-Kind <Connection| QueryTable>
[-PassThru]
[-Sheet <OfficeSheetSelector>]
-Xml <String>
[<CommonParameters>]#Parameter set:
XmlExamples
Add a Power Query definition
Open-OfficeExcel -Path .\Reports\Sales-2026.xlsx {
ExcelConnection -Name SalesQuery -CommandText 'let Source = Excel.CurrentWorkbook(){[Name="Orders"]}[Content] in Source'
}Stores a Power Query query that Excel can refresh; OfficeIMO writes the definition and never runs it.
Ship a workbook that refreshes from the warehouse
$query = 'let Source = Sql.Database("dw.example.com", "Sales"), Orders = Source{[Schema="dbo", Item="Orders"]}[Data] in Orders'
Open-OfficeExcel -Path .\Reports\Live-Sales.xlsx {
ExcelConnection -Sheet Data -Name WarehouseOrders -CommandText $query -Description 'Orders from the sales warehouse' -QueryTable OrdersQuery -RefreshOnOpen
}Adds a described query bound to a query table on the Data sheet and marks it to refresh when the workbook opens.
Parameters
- -CommandText String
- Power Query M expression stored as metadata.
- -Description String
- Connection description.
- -Document ExcelDocument
- Caller-owned workbook; omission uses the enclosing Excel content block.
- -Name String
- Connection name stored in workbook metadata.
- -PassThru SwitchParameter
- Emits the native result after the operation.
- -QueryTable String
- Native query-table metadata used by the connection.
- -RefreshOnOpen SwitchParameter
- Request refresh-on-open metadata.
- -Sheet OfficeSheetSelector
- Worksheet name, one-based position or native worksheet object. Multi-sheet commands also accept wildcard names.
- -Kind OfficeExcelPackageMetadataKind
- Metadata kind to add.
- Possible values:
Connection,QueryTable - -Xml String
- XML payload to add as package metadata.
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.