Write an array to a file:
PS C:\Users\dharmatech\Downloads> 10, 20, 30 | Out-File 'test.txt'
Read the array back in and call ConvertTo-Json on it:
PS C:\Users\dharmatech\Downloads> ConvertTo-Json (Get-Content .\test.txt)
The result is not the JSON version of the array... See the result below.
If you call Get-Member on one of the strings in the array read from the file, you'll see that the string object has been annotated with a few NotePropertys. I'm guessing that explains the ConvertTo-Json output.
What's a good way to get the JSON version of the array in the file, using ConvertTo-JSON?
[
{
"value": "10",
"PSPath": "C:\\Users\\dharmatech\\Downloads\\test.txt",
"PSParentPath": "C:\\Users\\dharmatech\\Downloads",
"PSChildName": "test.txt",
"PSDrive": {
"CurrentLocation": "Users\\dharmatech\\Downloads",
"Name": "C",
"Provider": "Microsoft.PowerShell.Core\\FileSystem",
"Root": "C:\\",
"Description": "",
"Credential": "System.Management.Automation.PSCredential",
"DisplayRoot": null
},
"PSProvider": {
"ImplementingType": "Microsoft.PowerShell.Commands.FileSystemProvider",
"HelpFile": "System.Management.Automation.dll-Help.xml",
"Name": "FileSystem",
"PSSnapIn": "Microsoft.PowerShell.Core",
"ModuleName": "Microsoft.PowerShell.Core",
"Module": null,
"Description": "",
"Capabilities": 52,
"Home": "C:\\Users\\dharmatech",
"Drives": "C D"
},
"ReadCount": 1
},
{
"value": "20",
"PSPath": "C:\\Users\\dharmatech\\Downloads\\test.txt",
"PSParentPath": "C:\\Users\\dharmatech\\Downloads",
"PSChildName": "test.txt",
"PSDrive": {
"CurrentLocation": "Users\\dharmatech\\Downloads",
"Name": "C",
"Provider": "Microsoft.PowerShell.Core\\FileSystem",
"Root": "C:\\",
"Description": "",
"Credential": "System.Management.Automation.PSCredential",
"DisplayRoot": null
},
"PSProvider": {
"ImplementingType": "Microsoft.PowerShell.Commands.FileSystemProvider",
"HelpFile": "System.Management.Automation.dll-Help.xml",
"Name": "FileSystem",
"PSSnapIn": "Microsoft.PowerShell.Core",
"ModuleName": "Microsoft.PowerShell.Core",
"Module": null,
"Description": "",
"Capabilities": 52,
"Home": "C:\\Users\\dharmatech",
"Drives": "C D"
},
"ReadCount": 2
},
{
"value": "30",
"PSPath": "C:\\Users\\dharmatech\\Downloads\\test.txt",
"PSParentPath": "C:\\Users\\dharmatech\\Downloads",
"PSChildName": "test.txt",
"PSDrive": {
"CurrentLocation": "Users\\dharmatech\\Downloads",
"Name": "C",
"Provider": "Microsoft.PowerShell.Core\\FileSystem",
"Root": "C:\\",
"Description": "",
"Credential": "System.Management.Automation.PSCredential",
"DisplayRoot": null
},
"PSProvider": {
"ImplementingType": "Microsoft.PowerShell.Commands.FileSystemProvider",
"HelpFile": "System.Management.Automation.dll-Help.xml",
"Name": "FileSystem",
"PSSnapIn": "Microsoft.PowerShell.Core",
"ModuleName": "Microsoft.PowerShell.Core",
"Module": null,
"Description": "",
"Capabilities": 52,
"Home": "C:\\Users\\dharmatech",
"Drives": "C D"
},
"ReadCount": 3
}
]
PS C:\Users\dharmatech\Downloads>