Alezis
Alezis

Reputation: 1222

PowerShell string variable within array to array variable

I have a string variable like this:

$a = "{VAR1: 'value1', VAR2: 'value2', VAR3: 'value3'}"

I have found $ExecutionContext.InvokeCommand.ExpandString($variable) to execute string commands

Is there a way in PowerShell to get that string directly as an array?

Upvotes: 0

Views: 76

Answers (1)

Remko
Remko

Reputation: 7340

You can use ConvertFrom-Json for that:

$a = "{VAR1: 'value1', VAR2: 'value2', VAR3: 'value3'}"
$b = ConvertFrom-Json $a

Then you can access like this:

$b.VAR2

Or if you want an array of the values:

$MyArray = (ConvertFrom-Json $a).PSObject.Properties | select -ExpandProperty value

Upvotes: 1

Related Questions