arrayspowershellhashtable

What is the difference between @() and @{} in Powershell and when to use one over another?


I was going through some examples in powershell and see the usage of both @() and @{}. Doing a quick google search on the difference tells me that one is an array and another is hashtable.

Could you share some real-life examples where say I would like to build a hashtable and not an array? And pros/cons of one over another? Thanks in advance for your inputs!


Solution

  • An array is simply a list of values whereas a hashtable is a collection of key/value pairs.

    Here are some examples:

    Array

    $i = @(1,2,3,4,5)
    

    Hashtable

    [hashtable]$i = @{ Number = 1; Shape = "Square"; Color = "Blue"}