0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Read Excel File Easily

Use $excel = New-Object -ComObject Excel.Application to open Excel, then $workbook = $excel.Workbooks.Open('path_to_file.xlsx') to load the file, and finally read cells with $worksheet.Cells.Item(row, column).Text.
📋

Examples

InputExcel file with A1=Hello, B1=World
OutputHello World
InputExcel file with A1=123, B1=456
Output123 456
InputEmpty Excel file
Output
🧠

How to Think About It

To read an Excel file in PowerShell, you create an Excel COM object to control Excel, open the file, select the worksheet, and then read the cell values by specifying their row and column numbers.
📐

Algorithm

1
Create an Excel COM object.
2
Open the Excel file using the COM object.
3
Select the first worksheet.
4
Read the desired cells by row and column.
5
Close the workbook and quit Excel.
💻

Code

powershell
$excel = New-Object -ComObject Excel.Application
$excel.Visible = $false
$workbook = $excel.Workbooks.Open('C:\temp\sample.xlsx')
$worksheet = $workbook.Sheets.Item(1)
$value1 = $worksheet.Cells.Item(1,1).Text
$value2 = $worksheet.Cells.Item(1,2).Text
Write-Output "$value1 $value2"
$workbook.Close($false)
$excel.Quit()
Output
Hello World
🔍

Dry Run

Let's trace reading cells A1 and B1 from the Excel file 'sample.xlsx'.

1

Create Excel COM object

$excel = Excel.Application COM object created

2

Open workbook

$workbook = sample.xlsx opened

3

Read cells

Read A1='Hello', B1='World'

CellValue
A1Hello
B1World
💡

Why This Works

Step 1: Create Excel COM object

The New-Object -ComObject Excel.Application command starts Excel invisibly so PowerShell can control it.

Step 2: Open Excel file

The Workbooks.Open method loads the Excel file so you can access its sheets and cells.

Step 3: Read cell values

Accessing Cells.Item(row, column).Text gets the text content of each cell.

Step 4: Clean up

Closing the workbook and quitting Excel frees resources and avoids leaving Excel running.

🔄

Alternative Approaches

ImportExcel Module
powershell
Import-Module ImportExcel
$data = Import-Excel -Path 'C:\temp\sample.xlsx'
$data | ForEach-Object { "$($_.Column1) $($_.Column2)" }
This method is simpler and faster but requires installing the ImportExcel module from PowerShell Gallery.
CSV Export and Import
powershell
Convert Excel to CSV manually, then use:
$data = Import-Csv -Path 'C:\temp\sample.csv'
$data | ForEach-Object { "$($_.Column1) $($_.Column2)" }
This avoids COM objects but needs manual CSV export and works only for simple data.

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

Time Complexity

Reading cells is linear in the number of cells accessed, so O(n) where n is the number of cells read.

Space Complexity

Memory usage grows with the number of cell values stored in variables, also O(n).

Which Approach is Fastest?

Using the ImportExcel module is faster and uses less memory than COM automation, which is slower and heavier.

ApproachTimeSpaceBest For
COM Object AutomationO(n)O(n)Full Excel features, no extra installs
ImportExcel ModuleO(n)O(n)Simple, fast, requires module install
CSV ImportO(n)O(n)Simple data, no Excel needed
💡
Always close the Excel COM object with $excel.Quit() to prevent Excel processes from lingering.
⚠️
Forgetting to close the workbook and quit Excel leaves hidden Excel processes running in the background.