Problem with arrays and hash tables

$list_of_hts = @();

$ht = @{};

$ht | Add-Member -MemberType NoteProperty -Name 'Name' -Value '1';
$ht | Add-Member -MemberType NoteProperty -Name 'CPU' -Value 'A';

$list_of_hts += $ht;

$ht | Add-Member -MemberType NoteProperty -Name 'Name' -Value '2' -Force;
$ht | Add-Member -MemberType NoteProperty -Name 'CPU' -Value 'B' -Force;

$list_of_hts += $ht;

Why array rewrote itself fully?

If you want to create a hashtable you simply do this:

$ht = @{
    Name = 1
    CPU  = 'A'

The cmdlet

is made for adding properties and methods to PowerShell objects … just like the documentation states.

What is it actually what you’re trying to accomplish? There might be another/better way.

The following documentation may help you …

If you want to provide a list of values for each key you may simply use a CSV input:

$InputData = @'
'@ |

The output looks like this

Name CPU
---- ---
1    A
2    B

And you could access the individual values with the dot notation and its index if you need:




for example. … or you use a loop to iterate over it … :man_shrugging:t3:

How can I insert my own values, for example, $A instead of “A”?

Just like Nick already pointed out in the answer to your new question … it is unclear what you’re talking about.

I already asked in your first thread … you may explain what it ACTUALLY is, what you’re trying to do. The bigger picture … not the way you think you have to go to achieve what you want.

You can insert whatever you want … :man_shrugging:t3: … if it’s literally $a you can do

$ht = @{
    Name = 1
    CPU  = '$A'

But in PowerShell the $ sign represents a variable. So it might be tricky sometimes if you need a literal $A.