0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Write Excel File Easily

Use PowerShell with COM object by creating an Excel application instance: $excel = New-Object -ComObject Excel.Application, then create workbook and write cells like $sheet.Cells.Item(1,1) = 'Hello' before saving with $workbook.SaveAs('path.xlsx').
📋

Examples

InputWrite 'Hello' in cell A1 and save as test.xlsx
OutputExcel file 'test.xlsx' created with 'Hello' in cell A1
InputWrite numbers 1 to 3 in cells A1 to A3 and save as numbers.xlsx
OutputExcel file 'numbers.xlsx' created with 1, 2, 3 in cells A1, A2, A3
InputWrite empty string in cell B2 and save as emptycell.xlsx
OutputExcel file 'emptycell.xlsx' created with empty cell B2
🧠

How to Think About It

To write an Excel file in PowerShell, you create an Excel application object using COM automation, add a workbook and worksheet, then assign values to specific cells by row and column. Finally, save the workbook to a file and close the Excel application to release resources.
📐

Algorithm

1
Create an Excel application COM object
2
Add a new workbook
3
Select the first worksheet
4
Write data to specific cells by row and column
5
Save the workbook to a file path
6
Close the workbook and quit the Excel application
💻

Code

powershell
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$workbook = $excel.Workbooks.Add()
$sheet = $workbook.Sheets.Item(1)
$sheet.Cells.Item(1,1) = 'Hello'
$workbook.SaveAs("$PWD\hello.xlsx")
$workbook.Close()
$excel.Quit()
Write-Output "Excel file saved as hello.xlsx"
Output
Excel file saved as hello.xlsx
🔍

Dry Run

Let's trace writing 'Hello' to cell A1 and saving the file

1

Create Excel COM object

$excel = New-Object -ComObject Excel.Application (Excel app created)

2

Add workbook and get sheet

$workbook = $excel.Workbooks.Add() (new workbook) $sheet = $workbook.Sheets.Item(1) (first sheet)

3

Write 'Hello' to cell A1

$sheet.Cells.Item(1,1) = 'Hello'

4

Save and close

$workbook.SaveAs("$PWD\hello.xlsx") $workbook.Close() $excel.Quit()

StepActionValue
1Create Excel COM objectExcel.Application instance
2Add workbook and get sheetWorkbook and Sheet objects
3Write to cell A1'Hello'
4Save and closeFile saved as hello.xlsx
💡

Why This Works

Step 1: Create Excel COM Object

Using New-Object -ComObject Excel.Application starts Excel invisibly so you can control it from PowerShell.

Step 2: Add Workbook and Select Sheet

You add a new workbook and select the first sheet to write data into cells.

Step 3: Write Data to Cells

Assign values to cells by specifying row and column with Cells.Item(row, column).

Step 4: Save and Close

Save the workbook to a file path, then close the workbook and quit Excel to free resources.

🔄

Alternative Approaches

Using ImportExcel Module
powershell
Install-Module -Name ImportExcel -Scope CurrentUser
$data = @{
  Name = 'John'; Age = 30
}
$data | Export-Excel -Path "$PWD\data.xlsx"
Simpler and faster without Excel installed, but requires installing a module.
Using CSV Export
powershell
$data = @(
  @{Name='John'; Age=30},
  @{Name='Jane'; Age=25}
)
$data | Export-Csv -Path "$PWD\data.csv" -NoTypeInformation
Exports data to CSV which Excel can open, but not a native Excel file.

Complexity: O(1) time, O(1) space

Time Complexity

Writing a few cells is constant time since no loops are involved; complexity grows linearly if writing many cells in loops.

Space Complexity

Uses constant extra memory for Excel COM objects; data is stored inside Excel application memory.

Which Approach is Fastest?

Using the ImportExcel module is faster and lighter than COM automation but requires module installation.

ApproachTimeSpaceBest For
COM AutomationO(1) for small writesO(1)Full Excel features, requires Excel installed
ImportExcel ModuleO(1)O(1)Quick Excel file creation without Excel installed
CSV ExportO(1)O(1)Simple data export, no Excel features
💡
Always call $excel.Quit() to close Excel and avoid hanging processes.
⚠️
Forgetting to close the Excel application leaves Excel running invisibly in the background.