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

PNG Press – Convert all those annoying .webp files quickly

PNG Press is a lightweight desktop application built using .NET Core 8. It features a simple drag-and-drop interface designed to convert one or multiple images to PNG format with ease. This project was born out of my own necessity for a straightforward image conversion tool, and I’m excited to finally share it with you.

Read More

Yu-Gi-Oh! Rarity Collection 2 Set Overview

The Yu-Gi-Oh! TCG set Rarity Collection 2 is getting mixed reviews to say the least. I’ve consulted with the Millennium items and searched the labyrinth of Atem’s mind to find this Yu-Gi-Oh! set’s stats, metrics, and other neat insights, let’s go!

Read More