How to convert JSON to CSV file using PowerShell?

To convert the JSON to the CSV format we need to first use the ConvertFrom-JSON command. For example, we have already JSON file present with us at the C:\temp\VMinfo.JSON location. We will first import this file and then convert it to CSV as shown below.

Get-Content C:\Temp\VMInfo.json | ConvertFrom-Json | Export-Csv C:\Temp\vminfo.csv -NoTypeInformation

Suppose you have cmdlet that produces output in the hash table format as shown below.

PS C:\> Get-AzVM -VMName TestMachine2k16 | Select -ExpandProperty Tags

Key             Value
---             -----
For             Ansible
Patching_Day    Sunday
Application     SecretTag
Owner           Chirag

So we first need to convert the hash output to the JSON format and then from the JSON, we can save the output to excel.

PS C:\> Get-AzVM -VMName TestMachine2k16 | Select -ExpandProperty Tags | ConvertTo-Json | ConvertFrom-Json | Export-Csv C:\temp\Vmtags.csv -NoTypeIn

Updated on: 06-Apr-2021

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started