Home > Software design >  Add data in a Excel Row
Add data in a Excel Row

Time:09-29

I try to create an excel file with some PSCustomObject. But i have a problem when i try to add data to this file. New Object erase old Object in my file if I don't use an array of object. It is possible without this array ?

In my code for example, $MyObject2 is present but not my $MyObject1:


$myObject1 = [PSCustomObject]@{
    "Hostname" = "W-1"
    "IPAdress" = "192.168.0.1" 
    "Domain" = "true"
    "Private" = "true"
    "Public" = "false"    
}

$myObject2 = [PSCustomObject]@{
    "Hostname" = "W-2"
    "IPAdress" = "192.168.0.2" 
    "Domain" = "true"
    "Private" = "false"
    "Public" = "false"  
    
}

$myObject1| Export-Excel -Path C:\Users\admin\Desktop\test1.xlsx -AutoSize
$myObject2 | Export-Excel -Path C:\Users\admin\Desktop\test1.xlsx -AutoSize
```

Result that I wish

[enter image description here][1]


  [1]: https://i.stack.imgur.com/PrfOg.png

CodePudding user response:

I think you need to provide a worksheet name for your output, so ...

$myObject1 | Export-Excel -Path C:\temp\test1.xlsx -AutoSize

becomes

$myObject1 | Export-Excel -Path C:\temp\test1.xlsx -AutoSize -WorksheetName Obj1

etc

  • Related