Repository navigation
ConvertTo-Json cmdlet hangs with -Depth 10 for object created using Get-Content #6388
Description
Activity
Hi ILAVELAN KANNIAPPAN (@ikanni) - I suspect it's not actually hanging - it's just descending very deeply into the object tree which takes a very, very long time. Just take a look at the output from:
ConvertTo-Json -InputObject $textContent -Depth 3
It's converting twoPSObjectswrapping strings. These objects have a lot of extra metadata added byGet-Content:[ { "value": "testline1", "PSPath": "C:\\Users\\brucepay.REDMOND\\AppData\\Local\\Temp\\test.txt", "PSParentPath": "C:\\Users\\brucepay.REDMOND\\AppData\\Local\\Temp", "PSChildName": "test.txt", "PSDrive": { "CurrentLocation": "Users\\brucepay.REDMOND\\AppData\\Local\\Temp", "Name": "C", "Provider": { "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\\brucepay.REDMOND", "Drives": "C D E", "VolumeSeparatedByColon": true }, "Root": "C:\\", "Description": "Windows", "MaximumSize": null, "Credential": { "UserName": null, "Password": null }, : :Each additional level of depth will introduce more fields that will then need to be serialized. To get to depth 10, you'll be nesting very deeply into the object tree and it will seem to hang. If you call
Get-Contentwith the-Rawflag, your example completes immediately:$textContent = Get-Content .\test.txt -Raw $json=ConvertTo-Json -InputObject $textContent -Depth 10Using
-RawtellsGet-Contentto not add all of the extra metadata, reducing the amount of work that needs to be done to serialize the objects.Agree with Bruce Payette (@BrucePay) that the cmdlet is doing what it is supposed to. Perhaps
Get-Contentshould be returning an array of strings and not objects (separate issue).The actual bug is that
ConvertTo-Jsondoesn't implement StopProcessing() to Ctrl+C doesn't break out.Perhaps
Get-Contentshould be returning an array of strings and not objects (separate issue).Get-Contentdoes return an array of strings, but it adds several NoteProperties to the string, includingPSDriveandPSProviderthat point to objects.> $s | gm TypeName: System.String Name MemberType Definition ---- ---------- ---------- Clone Method System.Object Clone(), System.Object ICloneable.Clone() ... PSChildName NoteProperty string PSChildName=test.txt PSDrive NoteProperty PSDriveInfo PSDrive=F PSParentPath NoteProperty string PSParentPath=F:\ PSPath NoteProperty string PSPath=F:\test.txt PSProvider NoteProperty ProviderInfo PSProvider=Microsoft.PowerShell.Core\FileSystem ReadCount NoteProperty long ReadCount=1Steve Lee (@SteveL-MSFT) Looking at the code, I don't think
StopProcessing()is appropriate because the thread is not blocked. Adding checks toStoppingin the inner loops should be sufficient.Dongbo Wang (@daxian-dbw) Right - it would still return
PSObjectsbut we could stop adding the extra metadata by default. This is a conceptually significant breaking change but in practice I don't think anyone would notice (except thatGet-Contentperformance and memory foot print would suddenly improve.)Reacted by Dongbo Wang- addedIssue-BugIssue has been identified as a bug in the productIssue has been identified as a bug in the product
on Mar 14, 2018 Marked as a bug for the need to support
Stoppingin the cmdlet.Dongbo Wang (@daxian-dbw) Bruce Payette (@BrucePay) Steve Lee (@SteveL-MSFT) Get-Content adds several NoteProperties, including PSDrive and PSProvider or extra metadata by default. Whether we will fix the Get-Content in future release to remove the several NoteProperties or extra metadata ? I understand it is breaking change.
ILAVELAN KANNIAPPAN (@ikanni) as a workaround you can do something like this:
Get-Content C:\path\to\file.txt | %{$_.PSObject.BaseObject} | ConvertTo-Json
I suggest opening a separate issue or locating an existing open issue and voting/commenting on it.
Reacted by Steve Lee and Thom Schumacher- addedWG-Cmdletsgeneral cmdlet issuesgeneral cmdlet issuesand removed
on Jul 17, 2026
Steps to reproduce
Expected behavior
Actual behavior
Environment data