1

When I run the command below

az group show --resource-group $GroupName

I get a line-by-line JSON describing the resource group with each property neatly indented and placed on each own row. Then, when I store the output into a variable and print it like this

$GroupDetails = az group show --resource-group $GroupName
Write-Host $GroupDetails

the contents of the JSON are scrambled into a single-line content. However, it's not that the formatting as such is gone because I still see a larger spacing in front of each property's name: the indenting is still there, only the line-breaks are ignored.

How can I store/print the value in a pretty manner?

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438
  • 2
    How can I store/print the value in a pretty manner? By removing `Write-Host` from the equation or piping instead: `$GroupDetails | Write-Host` – Santiago Squarzon May 07 '23 at 15:22
  • `Write-Host` (which is only designed for writing to the _display_ and which cannot output _data_), performs space-separated, single-line output formatting based on simple `.ToString()` calls, which with complex objects typically results in unhelpful representations. For richly formatted display-only output, use `Out-Host` instead; for data output, use `Write-Output` or, better yet, PowerShell's _implicit_ output feature. See the linked duplicates for details. – mklement0 May 07 '23 at 17:31
  • As an aside: `$GroupDetails = az group show --resource-group $GroupName` doesn't retain line breaks either; instead, it stores the output as an _array_ of _lines_ (this behavior applies to all PowerShell calls to _external programs_). If you wanted a single multiline string, you'd have to do ``$GroupDetails = (az group show --resource-group $GroupName) -join "`n"``, i.e. to re-join the individual lines with newlines. – mklement0 May 07 '23 at 17:35

0 Answers0