title: Powershell - Retain Complex objects with ConvertTo-Json
body: <p>Powershell command-let <em>ConvertTo-Json</em> has the following limitations
  1) It returns Enum values as Integers instead of their text
  2) It doesn't return the date in a readable format </p>
  
  <p>For point #1 see below, Status and VerificationMethod Properties</p>
  
  <pre><code>PS C:\Windows\system32&gt; Get-Msoldomain | ConvertTo-Json
  
  
  {
      "ExtensionData":  {
  
                        },
      "Authentication":  0,
      "Capabilities":  5,
      "IsDefault":  true,
      "IsInitial":  true,
      "Name":  "myemail.onmicrosoft.com",
      "RootDomain":  null,
      "Status":  1,
      "VerificationMethod":  1
  }
  </code></pre>
  
  <p>To handle this, I changed my command as below</p>
  
  <pre><code>PS C:\Windows\system32&gt; Get-Msoldomain | ConvertTo-Csv | ConvertFrom-Csv | ConvertTo-Json
  
  {
      "ExtensionData":  "System.Runtime.Serialization.ExtensionDataObject",
      "Authentication":  "Managed",
      "Capabilities":  "Email, OfficeCommunicationsOnline",
      "IsDefault":  "True",
      "IsInitial":  "True",
      "Name":  "curtisjmspartnerhotmail.onmicrosoft.com",
      "RootDomain":  "",
      "Status":  "Verified",
      "VerificationMethod":  "DnsRecord"
  }
  </code></pre>
  
  <p>Now you see, that the enums are being returned with their text values above (Status and VerificationMethod) instead of their integer values.</p>
  
  <p>However, There are a few limitations with this approach:</p>
  
  <blockquote>
    <p>1) ConvertTo-Csv doesn't retain the Arrays or Complex Objects, and
    outputs them as their Class Names (Watch the ExtensionData Properties
    in both the outputs). In the second output, we tend to lose the data,
    and just get the className
    <strong>System.Runtime.Serialization.ExtensionDataObject</strong> as a string</p>
    
    <p>2) Both ConvertTo-Csv and ConvertFrom-Csv are not the script-level
    commandlets, but they are command-level commandlets, which means that
    we can't use them at the end of the script , but they will have to be
    used with the individual commands like I am doing above. WHEREAS,
    ConvertTo-Json need not be applied at the commmandLevel, but just
    applied once for the script output.</p>
  </blockquote>
  
  <p>My question is:</p>
  
  <p>1) How do I still use the convertTo-Json, so that all my enum properties are returned with their texts and not integers, and ALSO the Complex Objects or Arrays are not lost? In the approach I have used, the complex objects are getting lost</p>
  
  <p>2) Also, it should be generic enough so that It can be applied at the end of the script, and not at the command level </p>