views:

296

answers:

4

I am writing a function that returns an id, name pair.

I would like to do something like

$a = get-name-id-pair()
$a.Id
$a.Name

like is possible in javascript. Or at least

$a = get-name-id-pair()
$a["id"]
$a["name"]

like is possible in php. Can I do that with powershell?

+5  A: 

Yes. Use the following syntax to create them

$a = @{}
$a["foo"] = "bar"
JaredPar
+5  A: 

also

$a = @{'foo'='bar'}

or

$a = @{}
$a.foo = 'bar'
Doug Finke
+1  A: 

You can also do this:

function get-faqentry { "meaning of life?", 42 }
$q, $a = get-faqentry

Not associative array, but equally as useful.

-Oisin

x0n
+1  A: 
#Define an empty hash
$i = @{}

#Define entries in hash as a number/value pair - ie. number 12345 paired with Mike is   entered as $hash[number] = 'value'

$i['12345'] = 'Mike'  
$i['23456'] = 'Henry'  
$i['34567'] = 'Dave'  
$i['45678'] = 'Anne'  
$i['56789'] = 'Mary'  

#(optional, depending on what you're trying to do) call value pair from hash table as a variable of your choosing

$x = $i['12345']

#Display the value of the variable you defined

$x

#If you entered everything as above, value returned would be:

Mike
Joshua