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”)

and

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

Thanks,
Kishor

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'                                                       
$serverList.GetType()                                                                                         

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


$Name= @{serverList = $null; serverListCount = $null}
$Name.GetType()                                                                                               

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
    {
        [CmdletBinding()]

        Param
        (
            [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
    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
docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-5.1

Simplify Your PowerShell Script with Parameter Validation
Simplify Your PowerShell Script with Parameter Validation - Scripting Blog

PowerShell – Passing Parameters as Variables using Remote Management and Invoke-Command
blogs.technet.microsoft.com/ralphkyttle/2015/06/04/powershell-passing-parameters-as-variables-using-remote-management-and-invoke-command

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

Use Parameter Sets to Simplify PowerShell Commands
Use Parameter Sets to Simplify PowerShell Commands - Scripting Blog

Weekend Scripter: Use PowerShell to Find Dynamic Parameters
Weekend Scripter: Use PowerShell to Find Dynamic Parameters - Scripting Blog

Dynamic ValidateSet in a Dynamic Parameter
'docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions_advanced_parameters?view=powershell-5.1

However, your post title is ‘Add to array’

Here is a good write up on add / remove reltive to arrays
Adding and Removing Items from a PowerShell Array – Jonathan Medd's Blog

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