Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

Dear Stackoverflow(ers),

I have a small problem with powershell and Excel. I have an excel file with several rows and text in them. I would like to read every column and put the content to an array. My script works, but when I show the content of my array there is always one 0 in the beginning. Is there something wrong with my Excel-File or with my Script?

    $a = New-Object -comobject Excel.Application
    $a.Visible = $true
    $a.DisplayAlerts = $False

    $Workbook = $a.workbooks.open("C:\test.xls")
    $Sheet = $Workbook.Worksheets.Item("Auto")

       $row            = [int]2
       $KN             = @() # beginnt bei 2,1... 3,1... 4,1
       Do {$KN        += $Sheet.Cells.Item($row,1).Text ; $row = $row + [int]1} until (!$Sheet.Cells.Item($row,1).Text)

   $Workbook.Close()
   $a.Quit()
   [System.Runtime.Interopservices.Marshal]::ReleaseComObject($a)

   Write-Output $KN

Output:

0
7
7
7
7
7

Excel File looks like that:

A    B    C    D
7
7
7
7
7
7

I guess it's some simple error with my array, but I can't find it! Thanks!

Edit1: The problem lays in the:

  $KN = @()

Because if I stop filling the array with my Exceldata, it continues to show me the 0.

Edit2: Ok it's getting weird. If I enter $KN, after the Script to look into the content again, it shows the right content without the 0. Where does it come from, and why is it there? And why is it gone, when I look into the array manually?

share|improve this question
up vote 1 down vote accepted

Found it! Finally! Never thought about that, sorry for the post!

[System.Runtime.Interopservices.Marshal]::ReleaseComObject($a) 

wrote the 0. And in my script it looked like, it came from the write-output!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.