Sometimes, you just need to put some no nonsense JSON in your PowerShell script. It can also be helpful if you anticipate others viewing/modifying the script who may understand JSON’s syntax, but not PowerShell. Sure, it’s not “proper”, and it can really bloat your scripts height, but it works great!
Let’s say you have the following JSON data
{ "i_have_come_here": { "to": [ "kick ass", "chew bubblegum" ] } }
Inserting this JSON body into your PowerShell script is easy, wrap it with
@"
"@
@" { "i_have_come_here": { "to": [ "kick ass", "chew bubblegum" ] } } "@
Note how the last “@ is hugging the left margin? That’s important! Using this method requires that the “@ be positioned at the very start of the line, no spaces! Your ISE will probably complain if you try to do it that way
That’s it! Told you it would be easy.
You can use this to assign that JSON body to a variable, or put a variable IN the JSON
$UserInput = Read-Host -Prompt "What did you come here to do? " $JSON = @" { "i_have_come_here": { "to": [ "$UserInput", "chew bubblegum" ] } } "@ Write-Host "$JSON"
Which will return this in the console