1

I am struggling on this for hours now ...

Its a syntax question basically how do I make this work : $test = '{"Title": $name}' The $name is not recognize as a variable. this is a simple example the real line is here and the unrecognize var is the $li at the end:

Add-PnPPageWebPart -Page "Home.aspx" -DefaultWebPartType ContentRollup  -Section 3 -Column $counter -WebPartProperties '{"query": {"contentLocation": 4,"contentTypes": [1],"sortType": 1,"filters": [{"filterType": 1,"value": "","values": []}],"documentTypes": [1,2,3,10],"advancedQueryText": ""}, "listTitle": $li}'

Thanks to those who will help :)

2
  • Yes, switch the quotes to double quotes. Interpolation doesn't occur in single quotes. So either switch the quotes, or escape the double quotes """{...}""". Commented Aug 22, 2022 at 15:37
  • 1
    In short: In PowerShell, only "..." strings (double-quoted aka expandable strings) perform string interpolation (expansion of variable values and expressions), not '...' strings (single-quoted aka verbatim strings). With "..." quoting, if the string value itself contains " chars., escape them as `" or "", or use a double-quoted here-string. See the conceptual about_Quoting_Rules help topics. Commented Aug 22, 2022 at 15:57

1 Answer 1

1

Use a double-quoted here-string:

$test = @"
{"Title": $name}
"@

Or use a regular double-quoted string, and then escape the literal "'s, by either doubling them:

$test = "{""Title"": $name}"

... or by using a backtick `:

$test = "{`"Title`": $name}"
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.