Overview

  • [Object[]], [Array] List of values
  • [PSCustomObject] Custom entity of members and properties
  • [Hashtable] Dictionary of key-value pairs
  • [Collections.Specialized.OrderedDictionary] Ordered dictionary of key-value pairs

Create containers

[Int32[]] $numberArray = 0, 1, 2, 4, 7
[String[]] $wordArray = @(
    'I'
    'am'
    "here"
)
[PSCustomObject] 
$customObject = [PSCustomObject] @{
    Name = "Karl"
    ID = 45
}
[Hashtable] 
$hashtable = @{
    Name = "Karl"
    ID = 45
}
[Collections.Specialized.OrderedDictionary] 
$orderedMap = [ordered]@{
    FirstValue = "Karl"
    SecondValue = 45
}

Containers

Array

  • [Object[]], [Array] List of values

Create an array

[Int32[]] $numbers = @(0..2; 4; 7)
[Int32[]] $numbers = 0, 1, 2, 4, 7
[Int32[]] $numbers = 0..2 + 4 + 7
[String[]] $words = @(
    'I'
    'am'
    "here"
)

Iterate over it

$numbers | foreach {
    $_
}

Custom object

  • [PSCustomObject] Custom entity of members and properties

Create a custom object

[PSCustomObject] $object = [PSCustomObject] @{
    Name = "Karl"
    ID = 45
}

Access its properties

$object.ID

Iterate over it

$object.PSObject.Properties | foreach {
    $_.Name + "=" + $_.Value
}

Dictionary

  • [Hashtable] Dictionary of key-value pairs

Create a hashtable

[Hashtable] $hashtable = @{
    Name = "Karl"
    ID = 45
}

Access its properties

$hashtable.ID

Iterate over it

$orderedMap.Keys
$orderedMap.Values
$hashtable.GetEnumerator() | foreach {
    $_.Key + "=" + $_.Value
}

Ordered dictionary

  • [Collections.Specialized.OrderedDictionary] Ordered dictionary of key-value pairs

Create a ordered dictionary

[Collections.Specialized.OrderedDictionary] $orderedMap = [ordered]@{
    FirstValue = 12
    SecondValue = 45
}

Access its properties

$orderedMap.FirstValue
$orderedMap[0]

Iterate over it

$orderedMap.Keys
$orderedMap.Values
$hashtable.GetEnumerator() | foreach {
    $_.Key + "=" + $_.Value
}

Sources:

Related:

Tags:
PowerShell Objects - Handle, Import, Export, Filter and RegEx query objects