1

I'm new to PowerShell, and can't seem to figure out why the first method in the code below does not work, while the second method (commented out) does.

I'm trying to write the result of a database recordset to an Excel file, so i need a dynamically expandable array. I know how to do that with the first type of array, but not with the second type.

My problem would be solved if:

The code:

#need this to work around bug if you have a non us-locale and English excel: http://support.microsoft.com/default.aspx?scid=kb;en-us;320369
[System.Threading.Thread]::CurrentThread.CurrentCulture = "en-US" 

# This doesn't work
$values = @(('test1','test2'),('test3','test4'))

# This works
<#
$values = New-Object 'object[,]' 2,2
$values[0,0] = 'test1'
$values[0,1] = 'test2'
$values[1,0] = 'test3'
$values[1,1] = 'test4'
#>

$excel = New-Object -ComObject Excel.Application
$excel.Visible = $False
$xls_workbook = $excel.Workbooks.Add()
$range = $xls_workbook.sheets.item(1).Range("A1:B2")
$range.Value2 = $values

$xls_workbook.SaveAs($MyInvocation.MyCommand.Definition.Replace($MyInvocation.MyCommand.Name, "") + "test.xlsx")
$excel.Quit()
Wouter
  • 1,829
  • 3
  • 28
  • 34

2 Answers2

1

when I get the type for both variables:

$values1.GetType() 
$values2.GetType()

This is what I get:

IsPublic IsSerial Name                                     BaseType                                                                                                               
-------- -------- ----                                     --------                                                                                                               
True     True     Object[]                                 System.Array                                                                                                           
True     True     Object[,]                                System.Array   

So $values1 is one-dimensional and $values2 is two-dimensional. There is a related question here:

Powershell Multidimensional Arrays

Community
  • 1
  • 1
Anonimista
  • 742
  • 1
  • 5
  • 12
  • http://powershell.com/cs/blogs/tips/archive/2008/12/05/multidimensional-arrays.aspx – Wouter Oct 19 '12 at 14:03
  • Thanks, the post you mentioned led me to the link above, giving a part of the answer. The array of the second type can not be expanded once it's created. – Wouter Oct 19 '12 at 14:04
1

**Object[]** is jagged array or array of arrays

**Object[,]** is 2 dimensional array.

Differences "in nutshell"

And How to convert jagged>2d arrays

Vadim KOT
  • 86
  • 1
  • 2