Search code examples
powershellstring-interpolationquoting

variable inside brackets powershell


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 :)


Solution

  • 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}"