ConvertFrom StringData - PowerShell
Overview
The ConvertFrom-StringData
command converts a string containing serialized data into its original object representation. It reconstructs objects that were previously converted to strings using the ConvertTo-StringData
cmdlet, allowing you to restore data that was stored or transmitted in string form.
Syntax
ConvertFrom-StringData [-InputObject] <string> [[-Type] <Type>] [-DeserializationBinder <DeserializationBinder>] [-Version <Version>]
Parameters
- InputObject: The string containing the serialized data.
- Type (optional): The type of the object to deserialize. If not specified, the
TypeName
property in the serialized data is used to determine the type. - DeserializationBinder (optional): A custom
SerializationBinder
to use for resolving type names during deserialization. - Version (optional): The version of the .NET Framework that was used to serialize the data.
Options/Flags
None.
Examples
Simple Example: Convert a String to an Object
$serializedData = 'System.String,mscorlib,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089:Hello World'
$deserializedObject = ConvertFrom-StringData -InputObject $serializedData
$deserializedObject
Output:
Hello World
Advanced Example: Convert a String to an Object Using Custom Deserialization Binder
using System;
using System.Runtime.Serialization;
[Serializable]
public class CustomType
{
public string Name { get; set; }
}
$serializedData = 'CustomType,MyAssembly,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null:{"Name":"John Doe"}'
$deserializationBinder = New-Object -TypeName System.Runtime.Serialization.SerializationBinder -ArgumentList @{BindToType = {$Type.FullName -match $args[0]}}, @{Binder = {return [CustomType]::new()}}
$deserializedObject = ConvertFrom-StringData -InputObject $serializedData -DeserializationBinder $deserializationBinder
$deserializedObject.Name
Output:
John Doe
Common Issues
Cannot Deserialize Data
The data may be corrupted or not serialized correctly. Verify the serialization format and ensure that the original type is available for deserialization.
Type Not Found
If you are not specifying a type, ensure that the serialized data contains the TypeName
property. If the type is not found, try using a custom DeserializationBinder
to resolve the type name.
Integration
ConvertFrom-StringData
can be used with other PowerShell commands to process and manipulate objects that were stored in string form. For example, you can use it with Get-Content
to read data from a file:
$serializedData = Get-Content "data.bin"
ConvertFrom-StringData -InputObject $serializedData
Related Commands
ConvertTo-StringData
: Converts objects to strings for storage or transmission.Export-Clixml
: Serializes objects to XML.Import-Clixml
: Deserializes objects from XML.