Using a Hashtable to store Key/Value Pairs

This is a simple example of using a Hashtable to store key/value pairs. Rather than referencing a value in an array using a numeric index, we now have named (the key) human-readable values that we can use to reference a stored value inside the Hashtable:

#create hashtable
$states = @{}
#add a key/value pair
$states.Add("ExampleKey", "ExampleValue1")
#two ways of getting the value
write-host $states.Get_Item("ExampleKey")
write-host $states.ExampleKey
#update the value for the key 'ExampleKey'
$states.Set_Item("ExampleKey", "ExampleValue2")
#two ways of getting the value
write-host $states.Get_Item("ExampleKey")
write-host $states.ExampleKey

 

Export to CSV using PowerShell and Dynamic Data

This post explains how to export to CSV using PowerShell and dynamic data. I find that hashtables are quite useful for various scenarios when scripting in PowerShell. We can also utilise them when exporting data using Export-CSV.

I provide two approaches to this. Both approaches work by adding hashtables to an array. By default if you export an array of hashtables to an array you will lose the column ordering. With PowerShell 2 we can circumvent this issue by using ‘Select’ and specifying the order of the columns we would like to select:

$csvnamePS2 = "C:\Temp\" + (Get-Date -format "dd-MM-yyyy-HH-mm") + ".csv"
$global:csvarrayPS2 = @()
function Export-To-CSV()
{  
param (       
[string]$FirstName,
[string]$LastName
)
$wrapper = New-Object -TypeName PSObject -Property @{ FirstName = $FirstName; LastName = $LastName;} | Select FirstName, LastName
$global:csvarrayPS2 += $wrapper
}
Export-To-CSV "Captain" "Hook"
Export-To-CSV "Peter" "Pan"
$global:csvarrayPS2 | Export-Csv -NoTypeInformation -Path $csvnamePS2

With PowerShell 3 we can simplify this, by specifying the [ordered] type for our hashtable:

$csvnamePS3 = "C:\Temp\" + (Get-Date -format "dd-MM-yyyy-HH-mm") + ".csv"
$global:csvarrayPS3 = @()
function Export-To-CSV()
{  
param (       
[string]$FirstName,
[string]$LastName
)
$wrapper = [ordered]@{ FirstName = $FirstName; LastName = $LastName;}
$global:csvarrayPS3 += New-Object psobject -property $wrapper
}
Export-To-CSV "Captain" "Hook"
Export-To-CSV "Peter" "Pan"
$global:csvarrayPS3 | Export-Csv -NoTypeInformation -Path $csvnamePS3

A Powershell Array of Hashtables instead of a Multidimensional Array

This post provides the benefits of using a Powershell array of hashtables instead of a multidimensional Array.

When scripting and coding a solution we’re often required to read from back-end databases. Sometimes if we want to reduce the amount of ‘reads’ to the database (if, for example, we know the information isn’t going to change very frequently) or we want faster access by storing the data in-memory, we may choose to write all the information we’ve retrieved from the database into an array.

 

Once we’ve stored the data inside our (multidimensional) array, retrieving a particular record is sometimes an obstacle. Particularly when we’re using unnamed indexes to reference records and array dimensions. To circumvent this issue, I like to store my records in an (single dimension) array of hashtables! This allows me to retrieve records very easily based on specific hashtable names!

$allRecords = @()
$allRecords += (@{ 
value="alkane1";
text="solutions1";
additional="this is some additional data for my record";
enabled=$true;
})
$allRecords += (@{ 
value="alkane2";
text="solutions2";
additional="this is some additional data for my record";
enabled=$false;
})
#retrieve selected record from array
$selectedRecord = $allRecords | Where-Object { $_.value -eq "alkane1" }
if ($selectedRecord -ne $null)
{
write-host $selectedRecord.value
write-host $selectedRecord.text
write-host $selectedRecord.additional
write-host $selectedRecord.enabled
}