Reading Excel Cells

by Feb 28, 2018

Occasionally, you may have to read information from Excel spreadsheets. PowerShell can access the Microsoft Excel object model albeit it is quite slow.

Here is example code that illustrates how you access Excel cells. Make sure you adjust the path in the code below so it points to a valid Excel file. The code then reads the content of A1 cell:

$excel = New-Object -ComObject Excel.Application
# open Excel file
$workbook = $excel.Workbooks.Open("c:\test\excelfile.xlsx")
 
# uncomment next line to make Excel visible
#$excel.Visible = $true
 
$sheet = $workbook.ActiveSheet
$column = 1
$row = 1
$info = $sheet.cells.Item($column, $row).Text
$excel.Quit()
 
 
"Cell A1 contained '$info'"

Are you an experienced professional PowerShell user? Then learning from default course work isn’t your thing. Consider learning the tricks of the trade from one another! Meet the most creative and sophisticated fellow PowerShellers, along with Microsoft PowerShell team members and PowerShell inventor Jeffrey Snover. Attend this years’ PowerShell Conference EU, taking place April 17-20 in Hanover, Germany, for the leading edge. 35 international top speakers, 80 sessions, and security workshops are waiting for you, including two exciting evening events. The conference is limited to 300 delegates. More details at www.psconf.eu.

Twitter This Tip! ReTweet this Tip!