Here’s an example of a PowerShell script that reads and edits a JSON file:
$jsonFilePath = "path/to/file.json" # Replace with the actual file path # Read the JSON file $jsonContent = Get-Content -Raw -Path $jsonFilePath | ConvertFrom-Json # Access and modify the desired properties $jsonContent.property1 = "New Value" $jsonContent.property2 = 12345 # Convert the JSON object back to a JSON string $jsonString = $jsonContent | ConvertTo-Json -Depth 10 # Write the modified JSON back to the file $jsonString | Set-Content -Path $jsonFilePath
Make sure to replace
"path/to/file.json" with the actual path to your JSON file.
In the script, we use the
Get-Content cmdlet to read the JSON file as a raw string. Then, we pipe it to the
ConvertFrom-Json cmdlet to convert it into a PowerShell object.
You can access and modify the properties of the JSON object as needed. In the example, we assign new values to
After making the modifications, we convert the JSON object back to a JSON string using the
ConvertTo-Json cmdlet. The
-Depth parameter specifies the depth of the object hierarchy in case it exceeds the default value (2).
Finally, we use the
Set-Content cmdlet to write the modified JSON string back to the file, overwriting its previous content.
Please ensure that you have appropriate permissions to read and write the JSON file.
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.