February 2019
Intermediate to advanced
626 pages
15h 51m
English
The ConvertTo-Json command can be used to convert a PowerShell object (or hashtable) into JSON:
PS> Get-Process -Id $PID |
Select-Object Name, Id, Path |
ConvertTo-Json
{
"Name": "powershell_ise",
"Id": 3944,
"Path": "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell_ise.exe"
}
By default, ConvertTo-Json will convert objects into a depth of two. Running the following code will show how the value for three is simplified as a string:
@{
one = @{ # 1st iteration
two = @{ # 2nd iteration
three = @{
four = 'value'
}
}
}
} | ConvertTo-Json
The three property is present, but the value is listed as System.Collections.Hashtable, as acquiring the value would need a third iteration. Setting the value of the Depth parameter ...
Read now
Unlock full access