Read and Get Values from Excel File using PowerShell Script

Monday, August 4, 2014

What do I use to read data or values from excel file?  And what if I want to read every row on multiple columns? Example data is like this....

NAME
AGE
CITY
John
22
Chicago
Edward
36
Houston
Michael
57
Brooklyn

First we declare the path where the excel file is stored.  Also, declare the sheet name.

$file = "C:\Documents\ExcelFolder\ExcelFile.xls"
$sheetName = "Sheet1"

After that, create an instance using the COM Object Excel Application to open the excel file.

$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open($file)
$sheet = $workbook.Worksheets.Item($sheetName)
$objExcel.Visible=$false

We will also need the number of Rows that has values. We will need these to loop and check every value on every column.

$rowMax = ($sheet.UsedRange.Rows).count

And now declare the starting position for each column

$rowName,$colName = 1,1
$rowAge,$colAge = 1,2
$rowCity,$colCity = 1,3

We will now loop through each row and store each variable that you can use for anything you want (i.e. e-mail body variables)

for ($i=1; $i -le $rowMax-1; $i++)
{
$name = $sheet.Cells.Item($rowName+$i,$colName).text
$age = $sheet.Cells.Item($rowAge+$i,$colAge).text
$city = $sheet.Cells.Item($rowCity+$i,$colCity).text

}

For the last line of code, we will close the excel file after opening and reading it. If we don’t close it, it will be locked for editing.

$objExcel.quit()

You can check the output by using Write-Host. For the full code below:

#Declare the file path and sheet name
$file = "C:\Users\kfeb\Documents\Textfile\ExcelFile.xlsx"
$sheetName = "Sheet1"
#Create an instance of Excel.Application and Open Excel file
$objExcel = New-Object -ComObject Excel.Application
$workbook = $objExcel.Workbooks.Open($file)
$sheet = $workbook.Worksheets.Item($sheetName)
$objExcel.Visible=$false
#Count max row
$rowMax = ($sheet.UsedRange.Rows).count
#Declare the starting positions
$rowName,$colName = 1,1
$rowAge,$colAge = 1,2
$rowCity,$colCity = 1,3
#loop to get values and store it
for ($i=1; $i -le $rowMax-1; $i++)
{
$name = $sheet.Cells.Item($rowName+$i,$colName).text
$age = $sheet.Cells.Item($rowAge+$i,$colAge).text
$city = $sheet.Cells.Item($rowCity+$i,$colCity).text

Write-Host ("My Name is: "+$name)
Write-Host ("My Age is: "+$age)
Write-Host ("I live in: "+$city)
}
#close excel file
$objExcel.quit()


8 Comments
Disqus
Fb Comments
Comments :

8 comments:

  1. Hi All,

    I am new to powershell. I would like to know the way to get the Name and City of all where age is 57.
    Is there any way other than oledb way to do so.
    Thanks in advance.

    ReplyDelete
    Replies
    1. put if statement inside for loop. if (age -eq 57){Write-Host...}

      Delete
  2. For me the script resulted in two errors: 1.Old format or invalid type library when creating the application object. My regional settings where not set to -en-us-. (I am dutch) After changing the settings I could create the object.
    2: it gave another error closing the object, locking my excelfile. after changing $objExcel.quit() to $objExcel.Quit() with capital Q I was all set.
    just what I needed!

    ReplyDelete
  3. Is there any way to use multiple sheets??

    ReplyDelete
    Replies
    1. i think you can put the excel files in folder then get the folder path and read through the files inside

      Delete
  4. I had to change $i=0 to grab 1st line
    for ($i=0; $i -le $rowMax-1; $i++)

    Thank you.

    ReplyDelete
  5. I copied the whole skropt in Powershell ISE, runned sucesfully but no text in the target file!?
    Any Idea?
    Thanks

    ReplyDelete
  6. how can i break the line during writting in any file.

    ReplyDelete