Search

How to: The easy (lazy) way of inserting JSON in your PowerShell scripts

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

Example of VS Code showing that there is a problem

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

Success!

Share the Post:

Related Posts

Yu-Gi-Oh! Legacy of Destruction TCG Set Overview

The Yu-Gi-Oh! TCG set Legacy of Destruction just exploded into the scene. I picked up the rubble and pieced together the data left behind from this set’s destructive capabilities. In my opinion, Legacy of Destruction sets the seeds for future deck tech, keep your eyes out for rogue strategies that will start to churn from this release. I think you will start to see some of these cards make their way into locals and competitive scene, once some of the unique strategies in this set are realized.

Read More

Yu-Gi-Oh Phantom Nightmare TCG Set Overview

Phantom Nightmare has been a meta-defining set in the Yu-Gi-Oh! TCG. Check out details about the set including various stat distributions, cards with the most text that everyone will definitely read and the other intriguing details!

Read More