How to display PowerShell results horizontally instead of vertically?

For example, you specified a variable as shown below.

$data1 = get-psdrive | where-object {$ -like 'c'} | select -expandproperty used

$data2 = get-psdrive | where-object {$ -like 'c'} | select -expandproperty free

echo $data1,data2

The output is vertical.



I used write-host -nonewline to display output horizontally, but the command does not export to txt

write-host $data1 -nonewline; write-host $data2 -nonewline >> c:\test.txt

How can I display horizontally and export in txt?


  • How about converting to json (or csv)? Note that ">>" can mix encodings, but add-content doesn't.

    get-psdrive c | select used, free | ConvertTo-Json -Compress | 
      add-content test.txt
    get-content test.txt

    Or just join them. Too bad select -expand doesn't work with multiple properties.

    psdrive c | % { ($_.used,$ -join ',' }