Skip to content

Format-Hashtable

SYNOPSIS

Converts a hashtable to its PowerShell code representation.

SYNTAX

Format-Hashtable [-Hashtable] <Object> [[-IndentLevel] <Int32>] [-ProgressAction <ActionPreference>]
 [<CommonParameters>]

DESCRIPTION

Recursively converts a hashtable to its PowerShell code representation. This function is useful for exporting hashtables to .psd1 files, making it easier to store and retrieve structured data.

EXAMPLES

EXAMPLE 1

$hashtable = @{
    Key1 = 'Value1'
    Key2 = @{
        NestedKey1 = 'NestedValue1'
        NestedKey2 = 'NestedValue2'
    }
    Key3 = @(1, 2, 3)
    Key4 = $true
}
Format-Hashtable -Hashtable $hashtable

Output:

@{
    Key1       = 'Value1'
    Key2       = @{
        NestedKey1 = 'NestedValue1'
        NestedKey2 = 'NestedValue2'
    }
    Key3       = @(
        1
        2
        3
    )
    Key4       = $true
}

PARAMETERS

-Hashtable

The hashtable to convert to a PowerShell code representation.

Type: Object
Parameter Sets: (All)
Aliases:

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

-IndentLevel

The indentation level for formatting nested structures.

Type: Int32
Parameter Sets: (All)
Aliases:

Required: False
Position: 2
Default value: 1
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

string

NOTES

A string representation of the given hashtable. Useful for serialization and exporting hashtables to files.

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