I am trying to import a csv file into Excel using Powershell. The $Range.Value2 assignment fails with a cast error.
I've boiled the issue down to the simplest example.
$excel = New-Object -ComObject Excel.Application
$excel.visible = $true
$workbook = $excel.Workbooks.Add()
$range = $workbook.ActiveSheet.Range("a1","b2")
$array = New-Object 'object[,]' 2,2
$array[0,0] = 'Cell1'
$array[0,1] = 'Cell2'
$array[1,1] = 'Cell3'
$array[1,0] = 'Cell4'
$range.Value2 = $array
Instead of inserting 'CellX' into a1, b1, a2, b2, I get --
PS C:\cygwin64\home\zkgf4jl\mfParser> C:\cygwin64\home\zkgf4jl\mfParser\Untitled2.ps1 Unable to cast object of type 'System.Object[,]' to type 'System.Management.Automation.PSReference`1[System.Management.Automation.LanguagePrimitives+Null]'. At C:\cygwin64\home\zkgf4jl\mfParser\Untitled2.ps1:10 char:1 + $range.Value2 = $array + ~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], InvalidCastException + FullyQualifiedErrorId : System.InvalidCastException