PowerShell ConvertFrom-Json: read JSON files and retrieve values
- Published: 2026.10.14
- Updated: 2026.10.14
- Windows
You can display a JSON file in PowerShell, yet extracting just a name or port number feels awkward while the content is still a string. To use those settings in a script, read the complete file and pass it to ConvertFrom-Json. You can then access values by their property names.
The examples below create a small JSON file and walk through properties, array items, and parsing errors. They use syntax that works in Windows PowerShell 5.1 and PowerShell 7.
Create a sample JSON file
Use a new working folder, separate from your own settings. These commands create json-read-demo and write a UTF-8 settings.json inside it. If the folder already exists, choose another name so you can try the example without overwriting existing files.
New-Item -ItemType Directory -Path .\json-read-demo -ErrorAction Stop | Out-Null
Set-Location -LiteralPath .\json-read-demo
@'
{
"name": "demo",
"enabled": true,
"ports": [8080, 8443]
}
'@ | Set-Content -LiteralPath .\settings.json -Encoding UTF8
JSON uses double quotes around property names and string values, and true or false for booleans. Its syntax differs from a PowerShell hashtable: writing @{ name = 'demo' } into a file does not make it valid JSON.
Convert the complete text and read properties
Get-Content -Raw reads the complete file as one string, which you can pass to ConvertFrom-Json. -LiteralPath uses the filename exactly as written. For a UTF-8 file, specify -Encoding UTF8 as shown here.
$config = Get-Content -LiteralPath .\settings.json -Encoding UTF8 -Raw |
ConvertFrom-Json -ErrorAction Stop
$config.name
$config.enabled
$config.ports[0]
Place a property name after the dot to retrieve its value. Since ports is an array, [0] selects its first item. In the output below, the JSON boolean true appears as PowerShell’s True.
demo
True
8080
For more about reading complete text versus individual lines, see Get-Content -Raw and line arrays. Passing the whole document, including its newlines, makes the JSON parsing step easier to follow. If your file uses a different encoding, the reading method needs to match that encoding.
Retrieve items from a JSON array
If the entire JSON document is an array enclosed in [...], assign the converted value to a variable before accessing its items. This example parses a string containing two objects, without reading a file.
$items = '[{"name":"alpha"},{"name":"beta"}]' | ConvertFrom-Json
$items.Count
$items[1].name
There are two items, so Count is 2. The second item’s name is beta.
2
beta
Windows PowerShell 5.1 and PowerShell 7 differ in how they emit a JSON array into the pipeline. Wrapping this example in @('...' | ConvertFrom-Json) makes the entire array one item of the outer array in 5.1. The same Count and index expressions then produce different results. Direct assignment is a suitable way to use this two-item example in both versions.
A script whose input can contain one item or no items also needs to account for the shape of those values. PowerShell 7 offers -NoEnumerate, while Windows PowerShell 5.1 does not. The two-item example above should not be assumed to cover every case where the item count changes.
Handle malformed JSON as an error
Malformed JSON, such as a document with a missing closing brace, causes a parsing error. If subsequent processing should stop, use -ErrorAction Stop and provide a failure path with try and catch.
try {
'{"name":' | ConvertFrom-Json -ErrorAction Stop | Out-Null
} catch {
'Invalid JSON'
}
The input is incomplete, so the string inside catch is printed.
Invalid JSON
Empty input and the JSON value null are not handled in the same way as this syntax error. Successful parsing also does not guarantee that required properties exist. A real settings loader needs to check that the input is not empty and that the properties and values it uses are present.
Comment handling varies by version too. Windows PowerShell 5.1’s ConvertFrom-Json rejects JSON containing comments. When sharing a file between 5.1 and 7, using JSON without comments and with double-quoted property names and strings keeps the format straightforward.
Find where reading fails
- If the file cannot be found, check the current working folder and the path passed to
-LiteralPath. - If characters look corrupted, match
-Encodingto the encoding used when the file was saved. - If conversion fails, inspect quotation marks, commas, brackets, and any comments when using 5.1.
- If a value is missing, inspect the property name, nesting level, and array index.
Following the file read, JSON conversion, and property access in order makes it easier to locate where a value changes. Once the small sample returns the values you expect, replace it with the JSON you want to use and narrow down any differences.
References
-
Previous article
PowerShell Get-Content -Raw: read a whole file or individual lines 2026.10.13
-
Next article
記事がありません