windowspowershellpowercli

How to display PowerShell results horizontally instead of vertically?


For example, you specified a variable as shown below.

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

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

echo $data1,data2

The output is vertical.

$data1

$data2

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?


Solution

  • 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
    
    {"Used":217365741568,"Free":21004943360}
    
    

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

    psdrive c | % { ($_.used,$_.free) -join ',' }
    
    217382371328,20988313600