Arrays (PowerShell)

From vwiki
Jump to navigation Jump to search

Creation

$array = @()                 # Create blank array
$array = @("one", "two", 3)  # Create an array with some values
$array += 34                 # Add value to end of array

To create an array (table) with column headings, initialise an array, then create a row variable with the column headings and add this row to the array. This is convenient when building a table of data within a loop eg

$table = @()
foreach ($instance in $set) {
    $row = "" | Select Heading1, Heading2, Heading3
    $row.Heading1 = "something"
    $row.Heading2 = "like"
    $row.Heading3 = "this"
    $table += $row
}

Add rows to an array

> $array = @()
> $row = "" | Select h1,h2,h3
> $row.h1 = "esx1"
> $row.h2 = "HBA1"
> $row.h3 = "LUN1"
> $array = $array + $row
> $row = "" | Select h1,h2,h3
> $row.h1 = "esx2"
> $row.h2 = "HBA1"
> $row.h3 = "LUN2"
> $array = $array + $row
> $array

h1                                      h2                                      h3
--                                      --                                      --
esx1                                    HBA1                                    LUN1
esx2                                    HBA1                                    LUN2

Select row from array

Using above array as example...

> if (($array |?{$_.h1 -eq "esx2"})) {"true"} else {"false"}
true
> if (($array |?{$_.h1 -eq "esx3"})) {"true"} else {"false"}
false
> $array |?{$_.h1 -eq "esx2"}

h1                                      h2                                      h3
--                                      --                                      --
esx2                                    HBA1                                    LUN2


> $array |?{$_.h1 -eq "esx2"} | Select -ExpandProperty h2
HBA1

Array Types

.NET Array Lists are far more flexible than PowerShell arrays. With ArrayLists you can easily Add and Remove members and generally enjoy much more flexibility when it comes to manipulating its contents. Despite showing a full range of available methods (when piped through a Get-Member), PS arrays generally don't have many available methods. To confirm the type that you have...

> $ArrayList.gettype()

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

> $PSArray.gettype()

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

To force the creation of a .NET array from a PowerShell CmdLet, create one in a fashion such as this...

$a = New-Object System.Collections.ArrayList                                        # Empty array
$a = New-Object System.Collections.ArrayList(,(Get-Content test.txt))               # Populated with contents of test.txt

Hashtables

$hash = @{}                                          # Create blank array
$hash["Name"] = "Value"                              # Add value to end of array
$hash.GetEnumerator() | Sort-Object -Property Name   # Sort hashtable

To loop through a hash table

$hash.Keys | % {
    Write-Host "key = $_"
    Write-Host "value = " + $hash.Item($_) 
}