You can edit a JSON configuration file in Windows 10 and Windows 11 without installing external tools by reading the file into memory with Get-Content -Raw, parsing it into a PowerShell object with ConvertFrom-Json, updating the target property using dot notation, and writing the modified object back to disk with ConvertTo-Json -Depth and Set-Content -Encoding UTF8. This procedure works in both the built-in Windows PowerShell 5.1 (powershell.exe) that ships with every Windows 10 and Windows 11 installation and cross-platform PowerShell 7 (pwsh.exe), though the two engines differ in how they handle comments, character encoding defaults, hash table conversion, and syntax validation.

A Windows desktop shows PowerShell backing up and validating a JSON configuration while an editor highlights the enabled cache setting. Prerequisites and Version Compatibility​

Before modifying an application or system configuration file, verify which PowerShell edition you are running and check the permissions on the target file.

  • Check your PowerShell version: Run $PSVersionTable.PSVersion in your terminal. Windows 10 and Windows 11 include Windows PowerShell 5.1 by default. If you have installed PowerShell 7 via Winget, the Microsoft Store, or an MSI package, it runs side-by-side as pwsh.exe.
  • Administrative privileges: If the JSON file resides in a protected directory such as C:\Program Files, C:\Program Files (x86), or C:\ProgramData, you must open Windows Terminal or PowerShell as an Administrator (Right-click -> Run as administrator) before you can save changes or create a backup in the same folder.
  • Application state: Close the application or stop the background Windows service that owns the JSON configuration file before editing it. Many applications lock their configuration files while running or overwrite on-disk changes with their in-memory state when they exit.
  • JSON comments limitation: According to Microsoft's ConvertFrom-Json documentation, Windows PowerShell 5.1 returns an error if the JSON file contains // or /* */ comments. PowerShell 6 and higher parse JSON files with comments without throwing an error, but they discard the comments from the output object—meaning any comments in your original file will be stripped when you serialize and save the object back to disk.
  • Encoding differences: In Windows PowerShell 5.1, -Encoding UTF8 writes files as UTF-8 with a Byte Order Mark (BOM). In PowerShell 6 and later, Set-Content defaults to utf8NoBOM (UTF-8 without a BOM). Most modern parsers accept UTF-8 with or without a BOM, but if a strict parser rejects a BOM edited in Windows PowerShell 5.1, an alternate save method using .NET is provided later in this guide.

Back Up, Parse, Modify, and Save a JSON Configuration File​

Follow these steps in order to safely back up a configuration file, inspect its structure, update an existing setting or add a new one, and write the changes back to disk without truncating nested settings.

1. Define the file path and create a timestamped backup​

Warning: Never pipe a modified JSON object back into the source file without creating a backup first. If ConvertTo-Json runs with insufficient serialization depth or encounters an interrupted write, it will overwrite your configuration file with truncated data.
  1. Open PowerShell (press Win + X and select Terminal or Windows PowerShell).
  2. Store the path to your target JSON file in a variable and create a backup copy in the same directory using Copy-Item:
Code:
$ConfigPath = "C:\AppConfig\appsettings.json"
$BackupPath = "$ConfigPath.bak_$(Get-Date -Format 'yyyyMMdd_HHmmss')"

Copy-Item -Path $ConfigPath -Destination $BackupPath -ErrorAction Stop
  1. Confirm the backup file exists before proceeding:

Get-Item -Path $BackupPath

Expected result: PowerShell displays the directory listing for the newly created .bak_YYYYMMDD_HHMMSS file with a file size matching the original.

2. Read the JSON file into a PowerShell object​

  1. Use Get-Content with the -Raw parameter and pipe the output to ConvertFrom-Json:

$Config = Get-Content -Path $ConfigPath -Raw -Encoding UTF8 | ConvertFrom-Json -ErrorAction Stop

The -Raw parameter instructs Get-Content to read the entire file as a single multi-line string rather than an array of line strings, which improves parsing performance and prevents line-splitting errors in Windows PowerShell 5.1.

2. Inspect the top-level properties of the parsed object by running:

$Config

  1. To inspect nested sections, use dot notation to drill down into the hierarchy:

$Config.Logging.LogLevel

Expected result: PowerShell outputs the key-value pairs as properties on a PSCustomObject. JSON objects become nested PSCustomObject instances, and JSON arrays become standard PowerShell arrays.

3. Modify an existing setting or add a new property​

Once the JSON structure is loaded into $Config, how you update it depends on whether the property already exists in the file.

Modify an existing property​

  1. Assign the new value directly using dot notation. PowerShell preserves data types during serialization, so pass strings in quotes, integers without quotes, and booleans using $true or $false:
Code:
# Update a string value
$Config.Logging.LogLevel.Default = "Warning"

# Update an integer value
$Config.ConnectionStrings.MaxPoolSize = 200

# Update a boolean value
$Config.FeatureFlags.EnableTelemetry = $false
  1. If you need to update an item inside a JSON array, reference it by its zero-based index or filter by a property name:
Code:
# Update the first element in a simple string array
$Config.AllowedHosts[0] = "internal.example.com"

# Update a specific object inside an array of objects
($Config.Endpoints | Where-Object { $_.Name -eq "PrimaryApi" }).Port = 8443

Add a new property that does not yet exist​

Because ConvertFrom-Json outputs a PSCustomObject by default, you cannot assign a brand-new property simply by typing $Config.NewSetting = "Value"—PowerShell will throw a PropertyNotFoundException (The property 'NewSetting' cannot be found on this object).

To add a new key-value pair to a PSCustomObject, use Add-Member:

$Config | Add-Member -MemberType NoteProperty -Name "MaintenanceMode" -Value $false -Force

The -Force switch ensures that if the property already exists, Add-Member overwrites its value instead of failing.

4. Serialize and save the updated JSON with sufficient depth​

Warning: By default, ConvertTo-Json has a -Depth limit of 2. Any nested objects deeper than two levels are flattened into their .NET type name string (such as "@{Default=Warning}" or "System.Object[]"). Worse, the resulting file is still syntactically valid JSON, so syntax checkers will not catch the data loss. Always specify -Depth explicitly when saving configuration files.
  1. Convert the $Config object back to a JSON string using ConvertTo-Json with an explicit -Depth parameter (up to a maximum of 100), and write it to disk with Set-Content:

$Config | ConvertTo-Json -Depth 20 | Set-Content -Path $ConfigPath -Encoding UTF8 -ErrorAction Stop

In PowerShell 7.1 and higher, ConvertTo-Json emits a warning if the input object's nesting exceeds the specified -Depth. In Windows PowerShell 5.1, truncation happens silently without any warning unless -Depth is set high enough to cover the entire tree. Setting -Depth 20 (or higher for complex schemas) prevents truncation for standard configuration files.

Verify That the Saved File Parses and Contains Your Changes​

After writing the file to disk, verify both that the JSON syntax is intact and that no nested levels were flattened into string representations during serialization.

Verification in Both Windows PowerShell 5.1 and PowerShell 7​

  1. Re-read the saved file from disk into a fresh variable and check the updated property:
Code:
$Verify = Get-Content -Path $ConfigPath -Raw -Encoding UTF8 | ConvertFrom-Json -ErrorAction Stop
$Verify.Logging.LogLevel.Default

Expected result: The command completes without errors and outputs your new setting (Warning).

  1. Check the raw file text for accidental depth truncation (@{ strings buried inside string values):

Select-String -Path $ConfigPath -Pattern '"@\{.*?\}"'

Expected result: The command returns no output. If it returns matches (and those strings were not intentionally part of your configuration), your -Depth value was too low, and you should restore from $BackupPath and re-run Step 4 with a higher -Depth value.

Additional Verification in PowerShell 7+ (Test-Json)​

If you are using PowerShell 6.1 or later (pwsh.exe), Microsoft includes the Test-Json cmdlet, which validates a JSON string against the RFC 8259 specification (and optionally against a JSON Schema file):

Get-Content -Path $ConfigPath -Raw | Test-Json

Expected result: PowerShell outputs True. Note that Test-Json is not available in Windows PowerShell 5.1.

Alternate Methods for Specific Edge Cases​

Method A: Edit Dynamic Keys Using -AsHashtable (PowerShell 7+)​

When working with JSON files where keys are dynamic, contain special characters or spaces, differ only by letter case, or require frequent additions and removals, converting the file to a hash table is cleaner than calling Add-Member repeatedly.

The -AsHashtable switch was introduced in PowerShell 6.0. Starting in PowerShell 7.3, ConvertFrom-Json -AsHashtable returns an OrderedHashtable, which preserves the exact key order of the original JSON file.

Code:
# Open PowerShell 7 (pwsh.exe)
$ConfigPath = "C:\AppConfig\appsettings.json"
$ConfigHash = Get-Content -Path $ConfigPath -Raw | ConvertFrom-Json -AsHashtable

# Modify or add keys directly using standard bracket or dot syntax
$ConfigHash["Logging"]["LogLevel"]["Default"] = "Error"
$ConfigHash["NewDynamicKey"] = "Enabled"

# Remove a key cleanly
$ConfigHash.Remove("DeprecatedSetting")

# Save back to disk with sufficient depth
$ConfigHash | ConvertTo-Json -Depth 20 | Set-Content -Path $ConfigPath -Encoding utf8NoBOM

Method B: Save UTF-8 Without BOM in Windows PowerShell 5.1​

Windows PowerShell 5.1 writes a 3-byte UTF-8 Byte Order Mark (EF BB BF) when you use Set-Content -Encoding UTF8 or Out-File -Encoding UTF8. While most Windows applications accept this, strict third-party daemons, Node.js tools, or Python scripts without UTF-8-SIG handling may fail to read the file after you edit it.

To save UTF-8 without a BOM in Windows PowerShell 5.1, use the .NET [System.IO.File]::WriteAllText method with an explicit UTF8Encoding($false) object:

Code:
$JsonOutput = $Config | ConvertTo-Json -Depth 20
$Utf8NoBom = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllText((Resolve-Path $ConfigPath).Path, $JsonOutput, $Utf8NoBom)

Troubleshooting and Rollback​

Common Failure Modes and Fixes​

  • Nested settings are replaced with "@{Key=Value}" in the saved file:
    You ran ConvertTo-Json without specifying -Depth, or the -Depth number was smaller than the nesting level of your JSON file. Restore your backup file immediately, then re-run the edit and pass -Depth 20 (or up to 100) to ConvertTo-Json.
  • ConvertFrom-Json : Invalid JSON primitive in Windows PowerShell 5.1:
    This error occurs for two common reasons:
    1. The JSON file contains // or /* */ comments (such as VS Code settings.json or .NET appsettings.json files with inline notes). Either run your script in PowerShell 7 (pwsh.exe), which accepts and strips comments, or remove the comments manually before parsing in Windows PowerShell 5.1.
    2. You omitted -Raw on Get-Content while parsing a file with formatting that failed line-by-line assembly. Always include Get-Content -Raw.
  • A single-element JSON array [ "value" ] turns into a scalar "value" after saving:
    PowerShell unrolls single-element arrays when passing objects across the pipeline (|).
    • If the entire root of the JSON file is a single-element array, pass -NoEnumerate to ConvertFrom-Json and -AsArray to ConvertTo-Json (both require PowerShell 7+).
    • In Windows PowerShell 5.1, pass the object using the -InputObject parameter instead of the pipeline: ConvertTo-Json -InputObject @($Config) -Depth 20.
  • Special characters (accents, symbols, or HTML tags like <, >, &, ') are escaped as \u0027 or \u003c:
    ConvertTo-Json uses Newtonsoft Json.NET in PowerShell 6+ and JavaScriptSerializer in Windows PowerShell 5.1. While \uXXXX sequences are valid JSON and parse back identically, if you need literal characters for human readability in PowerShell 6.2+, check the -EscapeHandling parameter on ConvertTo-Json (which defaults to Default and only escapes control characters, whereas EscapeHtml and EscapeNonAscii escape additional character sets). If non-ASCII characters appear corrupted (é), ensure you passed -Encoding UTF8 to both Get-Content and Set-Content.
  • The property '...' cannot be found on this object. Verify that the property exists and can be set:
    You attempted to assign a new key using $Config.NewKey = "value" on a PSCustomObject, or you misspelled an intermediate parent object name. Check the exact spelling and hierarchy with $Config | Format-List *, or use Add-Member -MemberType NoteProperty to create the new key.

Rollback Procedure​

If an application fails to start after you edit its JSON configuration file, or if verification shows that data was truncated or malformed, restore the backup copy you created in Step 1:

  1. Copy the backup file over the modified configuration file using Copy-Item -Force:

Copy-Item -Path $BackupPath -Destination $ConfigPath -Force -ErrorAction Stop

  1. Verify that the restored file parses cleanly:

Get-Content -Path $ConfigPath -Raw -Encoding UTF8 | ConvertFrom-Json -ErrorAction Stop

  1. Once the application starts normally and you have confirmed the configuration is working, you can remove the temporary backup file with Remove-Item -Path $BackupPath.