Formatting query results

By default each PowerShell cmdlet has predefined formatting of output making it easy to read. PowerShell also provides the flexibility to adjust the output or convert the cmdlet output to a different format with the following cmdlets:

Formatting Conversion
Format-Custom ConvertTo-Csv
Format-List ConvertTo-Html
Format-Table ConvertTo-Json
Format-Wide ConvertTo-Xml

Formatting examples

In this example we get a list of Azure VMs in our default subscription. The Get-AzureRmVM command defaults output into a table format.

ResourceGroupName          Name   Location          VmSize  OsType              NIC ProvisioningState
-----------------          ----   --------          ------  ------              --- -----------------
MYWESTEURG        MyUnbuntu1610 westeurope Standard_DS1_v2   Linux myunbuntu1610980         Succeeded
MYWESTEURG          MyWin2016VM westeurope Standard_DS1_v2 Windows   mywin2016vm880         Succeeded

If you would like to limit the columns returned you can use the Format-Table cmdlet. In the following example we get the same list of virtual machines but restrict the output to just the name of the VM, the resource group, and the location of the VM. The -Autosize parameter sizes the columns according to the size of the data.

Get-AzureRmVM | Format-Table Name,ResourceGroupName,Location -AutoSize
Name          ResourceGroupName Location
----          ----------------- --------
MyUnbuntu1610 MYWESTEURG        westeurope
MyWin2016VM   MYWESTEURG        westeurope

If you would prefer you can view information in a list format. The following example shows this using theFormat-List cmdlet.

Get-AzureRmVM | Format-List Name,VmId,Location,ResourceGroupName
Name              : MyUnbuntu1610
VmId              : 33422f9b-e339-4704-bad8-dbe094585496
Location          : westeurope
ResourceGroupName : MYWESTEURG

Name              : MyWin2016VM
VmId              : 4650c755-fc2b-4fc7-a5bc-298d5c00808f
Location          : westeurope
ResourceGroupName : MYWESTEURG

Converting to other data types

PowerShell also offers multiple output format you can use to meet your needs. In the following example we use the Select-Object cmdlet to get attributes of the virtual machines in our subscription and and convert the output to CSV format for easy import into a database or spreadsheet.

Get-AzureRmVM | Select-Object ResourceGroupName,Id,VmId,Name,Location,ProvisioningState | ConvertTo-Csv -NoTypeInformation

You can also convert the output into JSON format. The following example creates the same list of VMs but changes the output format to JSON.

Get-AzureRmVM | Select-Object ResourceGroupName,Id,VmId,Name,Location,ProvisioningState | ConvertTo-Json
        "ResourceGroupName":  "MYWESTEURG",
        "Id":  "/subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/MYWESTEURG/providers/Microsoft.Compute/virtualMachines/MyUnbuntu1610",
        "VmId":  "33422f9b-e339-4704-bad8-dbe094585496",
        "Name":  "MyUnbuntu1610",
        "Location":  "westeurope",
        "ProvisioningState":  "Succeeded"
        "ResourceGroupName":  "MYWESTEURG",
        "Id":  "/subscriptions/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/resourceGroups/MYWESTEURG/providers/Microsoft.Compute/virtualMachines/MyWin2016VM",
        "VmId":  "4650c755-fc2b-4fc7-a5bc-298d5c00808f",
        "Name":  "MyWin2016VM",
        "Location":  "westeurope",
        "ProvisioningState":  "Succeeded"