CmdID / PowerShell / Cmdlets / ConvertFrom
ConvertFrom-String
Extracts and parses structured properties from string content. PowerShell reference
From the PowerShell reference
7 parameters
Alias cfs
What it does
Extracts and parses structured properties from string content. The ConvertFrom-String cmdlet extracts and parses structured properties from string content. This cmdlet generates an object by parsing text from a traditional text stream. For each string in the pipeline, the cmdlet splits the input by either a delimiter or a parse expression, and then assigns property names to each of the resulting split elements. You can provide these property names; if you do not, they are automatically generated for you. The cmdlet's default parameter set, ByDelimiter, splits exactly on the regular expression delimiter. It does not perform quote matching or delimiter escaping as the Import-Csv cmdlet does. The cmdlet's alternate parameter set, TemplateParsing, generates elements from the groups that are captured by a regular expression. For more information on regular expressions, see about_Regular_Expressions. This cmdlet supports two modes: basic delimited parsing, and automatically-generated, example-driven parsing. Delimited parsing, by default, splits the input at white space, and assigns property names to the resulting groups. You can customize the delimiter by piping the ConvertFrom-String results into one of the Format-* cmdlets, or you can use the Delimiter parameter. The cmdlet also supports automatically-generated, example-driven parsing based on the FlashExtract, research work by Microsoft Research.
ConvertFrom-String [-Delimiter <String>] [-PropertyNames <String[]>] [-InputObject] <String> [<CommonParameters>]ConvertFrom-String [-TemplateFile <String[]>] [-TemplateContent <String[]>] [-IncludeExtent] [-UpdateTemplate] [-InputObject] <String> [<CommonParameters>]
Generate an object with default property names
"Hello World" | ConvertFrom-StringIdentifyExample 1A: Get to know the generated object
"Hello World" | ConvertFrom-String | Get-MemberIdentifyGenerate an object with default property names using a delimiter
"Contoso\Administrator" | ConvertFrom-String -Delimiter "\\"IdentifyGenerate an object that contains two named properties
$content = Get-Content C:\Windows\System32\drivers\etc\hosts
$content = $content -match "^[^#]"
$content | ConvertFrom-String -PropertyNames IP, ServerIdentifyUse an expression as the value of the TemplateContent parameter, save the results in a variable.
$template = @'
{Name*:Phoebe Cat}, {phone:425-123-6789}, {age:6}
{Name*:Lucky Shot}, {phone:(206) 987-4321}, {age:12}
'@
$testText = @'
Phoebe Cat, 425-123-6789, 6
Lucky Shot, (206) 987-4321, 12
Elephant Wise, 425-888-7766, 87
Wild Shrimp, (111) 222-3333, 1
'@
$PersonalData = $testText | ConvertFrom-String -TemplateContent $template
Write-Output ("Pet items found: " + ($PersonalData.Count))
$PersonalDataIdentifySpecifying data types to the generated properties
$template = @'
{[string]Name*:Phoebe Cat}, {[string]phone:425-123-6789}, {[int]age:6}
{[string]Name*:Lucky Shot}, {[string]phone:(206) 987-4321}, {[int]age:12}
'@
$testText = @'
Phoebe Cat, 425-123-6789, 6
Lucky Shot, (206) 987-4321, 12
Elephant Wise, 425-888-7766, 87
Wild Shrimp, (111) 222-3333, 1
'@
$PersonalData = $testText | ConvertFrom-String -TemplateContent $template
Write-Output ("Pet items found: " + ($PersonalData.Count))
$PersonalDataIdentifyIts page in the reference: ConvertFrom-String.
Parameters
-DelimiterSpecifies a regular expression that identifies the boundary between elements. Elements that are created by the split become properties in the resulting object. The delimiter is ultimately used in a call to the Split method of the type [System.Text.RegularExpressions.RegularExpression].-IncludeExtentIndicates that this cmdlet includes an extent text property that is removed by default.-InputObjectSpecifies strings received from the pipeline, or a variable that contains a string object.-PropertyNamesSpecifies an array of property names to which to assign split values in the resulting object. Every line of text that you split or parse generates elements that represent property values. If the element is the result of a capture group, and that capture group is named (for example, (?<name>) or (?'name') ), then the name of that capture group is assigned to the property.
If you provide any elements in the PropertyName array, those names are assigned to properties that have not yet been named.
If you provide more property names than there are fields, PowerShell ignores the extra property names. If you do not specify enough property names to name all fields, PowerShell automatically assigns numerical property names to any properties that are not named: P1, P2, etc.-TemplateContentSpecifies an expression, or an expression saved as a variable, that describes the properties to which this cmdlet assigns strings. The syntax of a template field specification is the following: {[optional-typecast]<name>:<example-value>}.-TemplateFileSpecifies a file, as an array, that contains a template for the desired parsing of the string. In the template file, properties and their values are enclosed in brackets, as shown below. If a property, such as the Name property and its associated other properties, appears multiple times, you can add an asterisk (*) to indicate that this results in multiple records. This avoids extracting multiple properties into a single record.-UpdateTemplateIndicates that this cmdlet saves the results of a learning algorithm into a comment in the template file. This makes the algorithm learning process faster. To use this parameter, you must also specify a template file with the TemplateFile parameter.