0

I'm trying to update a boolean value inside a JSON formated file using powershell but I'm not getting the required output.

From the below,

{
"setComputerName":  false,
"setWallpaper":  true
}

I would like to get the output as,

{
"setComputerName":  true,
"setWallpaper":  true
}

Below is my script,

$file = Get-Content 'C:\temp\Config.json' -raw | ConvertFrom-Json
$file = $file.setComputerName = 'true'
$file | ConvertTo-Json  | set-content 'C:\temp\Config1.json'
2
  • Hi. For future reference, it's always a good idea to post the incorrect output (or exception details) along with the desired output, so that people can get a better understanding of the problem. Commented Nov 5, 2018 at 20:56
  • @DeanOC I agree Commented Nov 6, 2018 at 17:25

1 Answer 1

9

All you have to do once you import your json is.

$file.setComputerName=$true
$file | ConvertTo-Json  | set-content 'C:\temp\Config1.json'

You were trying to set the value as a string and it needs to be boolean, so you need to use $true or $false in order to set those values.

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

This is it. Worked genuinely. Thanks for the heads up.
@CDP-cdp No problem at all!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.