0

I am writing a small script in powershell and want to build a variable in a particular format.

$name="abc"
$json = '{\"name\":\"$name\",\"projectId\":\"10034\"}'
$json

when I am printing the value the value appears to be -

{\"name\":\"$name\",\"projectId\":\"10034\"}

I want to the output to be -

{\"name\":\"abc\",\"projectId\":\"10034\"}

How can we place the variable value inside ""(double quotes) in this case.

Aatif Akhter
  • 2,126
  • 1
  • 25
  • 46
  • Maybe this would help: https://stackoverflow.com/questions/15113413/how-do-i-concatenate-strings-and-variables-in-powershell – Alex Aug 24 '21 at 07:12

1 Answers1

1

If you want string interpolation, you have to use double quotes. For using double quotes inside double quotes , you have to escape them with `(backquote).

$name="abc"
$json = "{\`"name\`":\`"$name\`",\`"projectId\`":\`"10034\`"}"
$json

The output is:

{\"name\":\"abc\",\"projectId\":\"10034\"}
Venkataraman R
  • 12,181
  • 2
  • 31
  • 58