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
"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.
ReTweet this Tip!
I use the ImportExcel module: github.com/.../ImportExcel
Thanks. I have used PSExcel (although I do wish the documentation was a little more robust).
Yes that is wy about 3 years ago PSExcel was born, as a bonus its also much faster than doing things like this in excel this oldfasioned way http://ramblingcookiemonster.github.io/PSExcel-Intro/
Doesn't excel have to be installed for this method to work?