How to use the ConvertFrom-StringData command in PowerShell?


The ConvertFrom-String command converts the String to the Hashtable format as shown below.

Example

PS C:\> "This is string" | ConvertFrom-String

Output

P1 P2 P3
-- -- --
This is string

In the above example, We haven’t specified any header so that the output is separated the delimiter by space P1, P2 and continuous. By default, this command separates the string with a ‘=’ delimiter as shown below.

Example

$stringhash = @"
   Name = Spooler
   Starttype = Manual
   Status = Stopped
"@
$stringhash | ConvertFrom-StringData

Output

Name      Value
----      -----
Status    Stopped
Starttype Manual
Name      Spooler

Another method we can use is by separating string Keys and values with a delimiter parameter. This parameter is only available in the PowerShell core 7.1 version.

Example

$stringhash = @"
   Name | Spooler
   Starttype | Manual
   Status | Stopped
"@
ConvertFrom-StringData -StringData $stringhash -Delimiter '|'

We can also use the below method.

PS C:\> $stringhash = "Name = Spooler `n StartType = Manual `n Status = Stopped"
PS C:\> ConvertFrom-StringData -StringData $stringhash
Name Value
---- -----
Status Stopped
Name Spooler
StartType Manual

Updated on: 30-Mar-2021

751 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements