Skip to content

ConvertTo-Hashtable

SYNOPSIS

Converts an object to a hashtable.

SYNTAX

ConvertTo-Hashtable [-InputObject] <PSObject> [-ProgressAction <ActionPreference>] [<CommonParameters>]

DESCRIPTION

Recursively converts an object to a hashtable. This function is useful for converting complex objects to hashtables for serialization or other purposes.

EXAMPLES

EXAMPLE 1

$object = [PSCustomObject]@{
    Name        = 'John Doe'
    Age         = 30
    Address     = [PSCustomObject]@{
        Street  = '123 Main St'
        City    = 'Somewhere'
        ZipCode = '12345'
    }
    Occupations = @(
        [PSCustomObject]@{
            Title   = 'Developer'
            Company = 'TechCorp'
        },
        [PSCustomObject]@{
            Title   = 'Consultant'
            Company = 'ConsultCorp'
        }
    )
}
ConvertTo-Hashtable -InputObject $object

Output:

Name                           Value
----                           -----
Age                            30
Address                        {[ZipCode, 12345], [City, Somewhere], [Street, 123 Main St]}
Name                           John Doe
Occupations                    {@{Title=Developer; Company=TechCorp}, @{Title=Consultant; Company=ConsultCorp}}

This returns a hashtable representation of the object.

PARAMETERS

-InputObject

The object to convert to a hashtable.

Type: PSObject
Parameter Sets: (All)
Aliases:

Required: True
Position: 1
Default value: None
Accept pipeline input: True (ByValue)
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

The function returns a hashtable representation of the input object, converting complex nested structures recursively.

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