Skip to content

ConvertTo-Metadata

SYNOPSIS

Serializes objects to PowerShell Data language (PSD1)

SYNTAX

ConvertTo-Metadata [[-InputObject] <Object>] [-AsHashtable] [[-Converters] <Hashtable>] [<CommonParameters>]

DESCRIPTION

Converts objects to a texual representation that is valid in PSD1, using the built-in registered converters (see Add-MetadataConverter).

NOTE: Any Converters that are passed in are temporarily added as though passed Add-MetadataConverter

EXAMPLES

EXAMPLE 1

$Name = @{ First = "Joel"; Last = "Bennett" }
@{ Name = $Name; Id = 1; } | ConvertTo-Metadata

@{ Id = 1 Name = @{ Last = 'Bennett' First = 'Joel' } }

Convert input objects into a formatted string suitable for storing in a psd1 file.

EXAMPLE 2

Get-ChildItem -File | Select-Object FullName, *Utc, Length | ConvertTo-Metadata

Convert complex custom types to dynamic PSObjects using Select-Object.

ConvertTo-Metadata understands PSObjects automatically, so this allows us to proceed without a custom serializer for File objects, but the serialized data will not be a FileInfo or a DirectoryInfo, just a custom PSObject

EXAMPLE 3

ConvertTo-Metadata ([DateTimeOffset]::Now) -Converters @{
   [DateTimeOffset] = { "DateTimeOffset {0} {1}" -f $_.Ticks, $_.Offset }
}

Shows how to temporarily add a MetadataConverter to convert a specific type while serializing the current DateTimeOffset. Note that this serialization would require a "DateTimeOffset" function to exist in order to deserialize properly.

See also the third example on ConvertFrom-Metadata and Add-MetadataConverter.

PARAMETERS

-InputObject

The object to convert to metadata

Type: System.Object
Parameter Sets: (All)
Aliases:

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

-AsHashtable

Serialize objects as hashtables

Type: System.Management.Automation.SwitchParameter
Parameter Sets: (All)
Aliases:

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

-Converters

Additional converters

Type: System.Collections.Hashtable
Parameter Sets: (All)
Aliases:

Required: False
Position: 2
Default value: @{}
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

System.String

NOTES

Back to top