Skip to content

Merge-Hashtable

SYNOPSIS

Merges multiple hashtables, applying overrides in sequence.

SYNTAX

Merge-Hashtable [-Main] <Hashtable> [-Overrides] <Hashtable[]> [-Force] [-ProgressAction <ActionPreference>]
 [<CommonParameters>]

DESCRIPTION

This function takes a primary hashtable ($Main) and merges it with one or more override hashtables ($Overrides). Overrides are applied in order, with later values replacing earlier ones if the same key exists. If the -Force switch is used, values will be overridden even if they are empty or $null. The resulting hashtable is returned.

EXAMPLES

EXAMPLE 1

$Main = @{
    Key1 = 'Value1'
    Key2 = 'Value2'
}
$Override1 = @{
    Key2 = 'Override2'
}
$Override2 = @{
    Key3 = 'Value3'
}
$Main | Merge-Hashtable -Overrides $Override1, $Override2

Output:

Name                           Value
----                           -----
Key1                           Value1
Key2                           Override2
Key3                           Value3

Merges $Main with two override hashtables, applying overrides in order.

EXAMPLE 2

$Main = @{
    Key1 = 'Value1'
    Key2 = 'Value2'
}
$Override = @{
    Key2 = ''
    Key3 = 'Value3'
}
$Main | Merge-Hashtable -Overrides $Override -Force

Output:

Name                           Value
----                           -----
Key1                           Value1
Key2
Key3                           Value3

Forces overriding even if the value is empty.

PARAMETERS

-Main

Main hashtable

Type: Hashtable
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

-Overrides

Hashtable with overrides. Providing a list of overrides will apply them in order. Last write wins.

Type: Hashtable[]
Parameter Sets: (All)
Aliases:

Required: True
Position: 2
Default value: None
Accept pipeline input: True (ByValue)
Accept wildcard characters: False

-Force

When specified, force override even if the value is empty or null.

Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

-ProgressAction

{{ Fill ProgressAction Description }}

Type: ActionPreference
Parameter Sets: (All)
Aliases: proga

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

CommonParameters

This cmdlet supports the common parameters: -Debug, -ErrorAction, -ErrorVariable, -InformationAction, -InformationVariable, -OutVariable, -OutBuffer, -PipelineVariable, -Verbose, -WarningAction, and -WarningVariable. For more information, see about_CommonParameters.

INPUTS

OUTPUTS

Hashtable

NOTES

A merged hashtable with applied overrides.

https://psmodule.io/Hashtable/Functions/Merge-Hashtable/