Add to array

Hi Team,
I get confused with the functionality of an array and variable parameter. Please explain me the difference /advantage of one on other below;

$serverList= @()
$serverList= (“Testserver1”,“Testserver2”,“Testserver3”)


[hashtable]$Name= @{“serverList” = $null; “serverListCount” = $null}
$serverList= (“Testserver1”,“Testserver2”,“Testserver3”)
$Name.serverList = $serverList


They’re not the same thing.
The former is an array. This is a “data structure that is designed to store a collection of items”.

The latter is a hash table, which is a different data structure that holds key-value pairs.

$serverList = 'Testserver1','Testserver2','Testserver3'                                                       

IsPublic IsSerial Name                                     BaseType                                                            
-------- -------- ----                                     --------                                                            
True     True     Object[]                                 System.Array                                                        

$Name= @{serverList = $null; serverListCount = $null}

IsPublic IsSerial Name                                     BaseType                                                            
-------- -------- ----                                     --------                                                            
True     True     Hashtable                                System.Object                                                       

A PowerShell array holds a list of data items. The data elements of a PowerShell array need not be of the same type, unless the data type is declared (strongly typed). Arrays are built using operators or casts. The way to build an array in PowerShell is to use the comma operator.

A variable parameter is use when you need to store a static value or the results of some other calculation/collection/operation. One can pass an array data as a parameter or even a named parameter. For Example:

    Function Show-Friends

            [string] $MainPerson, 
            [string[]] $Friends

        "Main person is ",$MainPerson
        'Friends count is : ' ,$Friends.Count

        foreach($Friend in $Friends)
            $MainPerson + " has a Friend named: ” ,$Friend


    Show-Friends -MainPerson:Smith  -Friends:@('Jon', 'Joshua')
    Show-Friends Smith ('Jon', 'Joshua')

However, your second block is not a ‘variable parameter’ as noted by you, it’s a hastable. Hash Tables (also known as Associative arrays or Dictionaries) are a type of array that allows the storage of paired Keys and Values, rather like a simple database table. Unlike normal arrays where you refer to each element via a numeric index, the keys of a hash table can be strings. The Key values do still need to be unique, if you try to add the same key twice PowerShell will return an error (“item has already been added”).

For a review regarding variable and parameters, then look here:

About Functions Advanced Parameters

Simplify Your PowerShell Script with Parameter Validation

PowerShell – Passing Parameters as Variables using Remote Management and Invoke-Command

Use the Get-Command PowerShell Cmdlet to Find Parameter Set Information

Use Parameter Sets to Simplify PowerShell Commands

Weekend Scripter: Use PowerShell to Find Dynamic Parameters

Dynamic ValidateSet in a Dynamic Parameter

However, your post title is ‘Add to array’

Here is a good write up on add / remove reltive to arrays

Thank you all and especially Postanote for detailed explanation…I am happy now, heading further deep into understanding this :-)!!!