Search
featured image easy JSON

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

The Infinite Forbidden Set Stats

Yu-Gi-Oh! The Infinite Forbidden launched in the TCG on July 19, 2024. I’ve been excited for this set since its announcement because of the nostalgia-bait of Exodia. As a core set, it’s full of new cards, strategies, and archetypes. The highly anticipated Fiendsmith makes its TCG debut just in time for the Yu-Gi-Oh! NAWCQ. Exodia, White Forest, and Gimmick Puppet get support, and a new TCG-exclusive archetype called Mimighoul makes its debut.

Read More

Help! I’m Missing haptic feedback in Beat Saber

The haptic feedback in Beat Saber just stopped working. I knew it wasn’t the controller since vibrations were working outside of Steam or Beat Saber. I didn’t realize how much the haptic feedback contributed to VR immersion, but wow, did I miss it.

Read More