Skip to main content

PowerShell helper function to write an item to Hashtable, optionally overwriting the item if it is already contained with the Hashtable.

function Add-ToHashtable
{
    <#
    .SYNOPSIS
        Add item(s) to hashtable helper.

    .DESCRIPTION
        A helper function for adding items to a hashtable.

    .PARAMETER Hashtable
        The target hashtable.

    .PARAMETER Items
        A hashtable of items to add to the target hashtable.

    .PARAMETER Key
        The item key.

    .PARAMETER Value
        The item value.

    .PARAMETER NoOverwrite
        If present, do not overwrite an existing item in the hashtable with the same key/name.

    .PARAMETER PassThru
         If present, writes the hashtable object to output as the final operation.

    .EXAMPLE
        PS > Add-ToHashtable -Hashtable @{'One' = 1} -Key 'One' -Value 2 -NoOverwrite -PassThru

        Name                           Value
        ----                           -----
        One                            1

        The above example will not overwrite the existing item with the same key/name ('One').
    #>
    [CmdletBinding()]
    [OutputType([System.Void], [Hashtable])]
    param (
        [Parameter(Position = 0)]
        [Hashtable]
        $Hashtable,

        [Parameter(ParameterSetName = 'MultipleItems', Mandatory)]
        [Hashtable]
        $Items,

        [Parameter(ParameterSetName = 'SingleItem', Mandatory)]
        [Alias('Name')]
        $Key,

        [Parameter(ParameterSetName = 'SingleItem')]
        $Value,

        [Parameter()]
        [Switch]
        $NoOverwrite,

        [Parameter()]
        [Switch]
        $PassThru
    )

    function AddToHashtableInternal
    {
        param ([Hashtable] $_HashTable, $_Key, $_Value, [Switch] $_NoOverwrite)

        if ($_NoOverwrite)
        {
            if (-not $_HashTable.Contains($_Key))
            {
                $_HashTable.Add($_Key, $_Value)
                Write-Verbose ("Add-ToHashtable> Added unique item '{0}'" -f $_Key)
            }
            else
            {
                Write-Verbose ("Add-ToHashtable> Item '{0}' already exists, and -NoOverwrite was specified" -f $_Key)
            }
        }
        else
        {
            $_HashTable[$_Key] = $_Value
            Write-Verbose ("Add-ToHashtable> Added item '{0}'" -f $_Key)
        }
    }

    if ($null -eq $Hashtable)
    {
        if (-not $PassThru)
        {
            Write-Error ("Add-ToHashtable> You did not specify -PassThru (switch) and your -Hashtable value was null. Specify -PassThru to output a (new) hashtable comprised of this command's specified key/value parameters." -f $Key) -ErrorAction 'Continue'
            return
        }

        Write-Verbose ('Add-ToHashtable> Hashtable parameter was null, creating new hashtable' -f $Key)
        $Hashtable = @{}
    }

    if ($PSCmdlet.ParameterSetName -eq 'MultipleItems')
    {
        foreach ($item in $Items.GetEnumerator())
        {
            AddToHashtableInternal -_HashTable $Hashtable -_Key $($item.Name) -_Value $($item.Value) -_NoOverwrite:$NoOverwrite
        }
    }
    else
    {
        AddToHashtableInternal -_HashTable $Hashtable -_Key $Key -_Value $Value -_NoOverwrite:$NoOverwrite
    }

    if ($PassThru)
    {
        Write-Output -InputObject $Hashtable
    }
}