ConvertFrom String - PowerShell


ConvertFrom-String is a PowerShell cmdlet that converts a string representation of an object to an object of the specified type. It is useful for deserializing data from text or creating objects programmatically.


ConvertFrom-String -String <string> -Type <type> [-Delimiter <delimiter>] [-Format <format>] [-Culture <culture>] [-TimeZone <timeZone>]


The string representation of the object to be converted.

The type of the object to be created. This can be a .NET type (e.g., [System.Int32]) or a custom type.

The delimiter that separates the elements in the string. Defaults to a comma.

The format of the string representation. Valid values are:

  • Default: The format is inferred from the context.
  • Xml: The string is in XML format.
  • Json: The string is in JSON format.
  • Csv: The string is in CSV format.

The culture to use when converting the string. Defaults to the current culture.

The time zone to use when converting the string. Defaults to the current time zone.


Example 1: Convert a comma-separated string to an array of integers

$string = "1,2,3,4,5"
$array = ConvertFrom-String -String $string -Type [int[]]

Example 2: Convert an XML string to an XML document

$xmlString = '<root><element>value</element></root>'
$xmlDocument = ConvertFrom-String -String $xmlString -Format Xml -Type [System.Xml.XmlDocument]

Example 3: Convert a JSON string to a PowerShell object

$jsonString = '{"Name": "John Doe", "Age": 30}'
$object = ConvertFrom-String -String $jsonString -Format Json

Common Issues

  • Invalid type: Ensure that the specified type matches the format of the input string.
  • Invalid format: The input string must be in the specified format or the conversion will fail.
  • Missing delimiter: If a delimiter is specified, the input string must contain the delimiter.


ConvertFrom-String can be integrated with other PowerShell commands to create complex data processing pipelines. For example, you can use ConvertFrom-String to parse data from a file and then use Export-Csv to export the data to a CSV file.

$data = Get-Content -Path "data.txt"
$objects = ConvertFrom-String -String $data -Type [MyCustomType]
Export-Csv -InputObject $objects -Path "data.csv"