Problem with array and string

$Array = @();
$line = "" | select Name,CPU,Cores;

$line.Name = "VM1"
$line.CPU = "AMD"
$line.Cores = "4"
$Array += $line

$line.Name = "VM2"
$line.CPU = "Intel"
$line.Cores = "2"
$Array += $line

$Array

And I will get:

Name CPU   Cores
---- ---   -----
VM2  Intel 2
VM2  Intel 2

What I do wrong?

Georgy,

When posing questions to folks, it’s important to remember that we don’t necessarily know what you’re trying to do. We can make guesses, but it’s best to assume we know nothing and fully explain what your desired result is. Here, it seems like you’re trying to build an array, but you are adjusting the same object properties, and the end result only shows the intel system. This is likely a result of how Powershell handles ‘complex’ objects. They are passed by reference (by default) and not by value.

I’d suggest this approach:

$Array = @()

$Object = [PSCustomObject]@{
Name = 'VM1'
CPU = 'AMD'
Cores = 4
}

$Array += $Object

$Object = [PSCustomObject]@{
    Name = 'VM2'
    CPU = 'Intel'
    Cores = 2
    }

$Array += $Object

$Array

I generally try to avoid the re-use of the same variable names, but for the sake of simplicity I kept most of your code mostly the same.The code above I believe should output what you are expecting.