0

I think what I am after is an associative array here but I'm not too sure.

Quite simple what I want to do is this

 ServiceArray
 ben     gold
 harry   red
 joe     green
 tim     yellow

I want to have that in an array that I can call so $servicearray.ben would return gold.

Anyone have any pointers?

Thanks in advance

Trinitrotoluene
  • 1,388
  • 5
  • 20
  • 39

1 Answers1

3

You want a HashTable:

$ServiceArray = @{
   Ben = 'gold';
   Harry = 'red';
   Joe = 'green';
   Time = 'yellow';
 }
$ServiceArray.Ben

# Result:
# gold

If you want more "columns", you can create a HashTable of PSCustomObject. You can build PSCustomObject objects using a HashTable, as @mjolinor mentioned.

$ObjectList = @{
    Ben = 
    [PSCustomObject]@{
        Color = 'Gold';
        Shape = 'Square';
    };
    Harry =
    [PSCustomObject]@{
        Color = 'Red';
        Shape = 'Round';
    };
    Joe =
    [PSCustomObject]@{
        Color = 'Green';
        Shape = 'Triangle';
    };
    Tim =
    [PSCustomObject]@{
        Color = 'Yellow';
        Shape = 'Rectangle';
    };
};

# Syntax: Use dot-notation to access "Ben"
$ObjectList.Ben.Color; # Gold
$ObjectList.Ben.Shape; # Square

# Different syntax: Use the string indexer to access "Ben"
$ObjectList['Ben'].Color; # Gold
$ObjectList['Tim'].Shape; # Rectangle