Functions/Search-HashTable.ps1

<#
.SYNOPSIS
    This function performs a search of a hash table using a provided key.
.DESCRIPTION
    This function performs a search of a hash table using a provided key.
    It returns the value matching the key if found, and null otherwise.
    The case sensitive search has O(1) time complexity while the case insensitive search has
    O(N) time complexity, where N is the number of key-value pairs in the hash table.
#>

function Search-HashTable {
    [CmdletBinding()]
    param (
        # The hash table
        [Parameter(Mandatory=$true, ValueFromPipeline=$true)]
        [ValidateNotNull()]
        [HashTable]$hashTable,

        # The key used to lookup the value in the hash table
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [String]$key,

        # Select if the search should be case sensitive.
        [Parameter(Mandatory=$false)]
        [Switch]$caseSensitive
    )

    # Case sensitive search
    if ($caseSensitive) {
        return $hashTable.$key
    }

    # Case insensitive search
    else {
        foreach ($keyValuePair in $hashTable.GetEnumerator()) {
            # String comparisons are case insensitive
            if ($key -eq $keyValuePair.Key) {
                return $keyValuePair.Value
            }
        }
    }
}