Export-csv column format

Welcome Forums General PowerShell Q&A Export-csv column format

This topic contains 3 replies, has 3 voices, and was last updated by

3 years, 10 months ago.

  • Author
  • #24174

    Points: 0
    Rank: Member

    I'm trying to export to csv all disks for each computer from my list. I have computers configured with one, tow of tree disks. Now when I export the result of my script to csv file,I get it in formatted as list format:

    ComputerName, Disk
    Comp1, C:
    Comp1, D:
    Comp2, C:
    Comp2, D:
    I would like to know if it is possible to export this list but as table? If there are more then one disk in computer add a column to the table, something like this:

    ComputerName, Disk1, Disk2...
    Comp1, C:, D:
    Comp2, C:, D:
    Comp3, C:

    Here is my script :

    $computers = 'Comp1','Comp2'

    $out_csv =@()

    foreach ($computer in $computers) {

    $disks = Get-WmiObject -Class win32_logicaldisk -ComputerName $computer| select deviceid, volumename

    foreach ($disk in $disks) {

    $ou = New-Object -TypeName psobject -Property @{

    'Computer' = $computer
    'drive' = $disk.volumename
    'ID' = $disk.deviceid

    $out_csv += $ou

    $out_csv | Export-Clixml -Path 'C:\Users\Administrator\disk.csv'


  • #24175

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    Actually, the way you're doing it now is the better, "right-er" way to do it. What you're producing is technically hierarchical data, where every computer has one or more drives. CSV sucks for hierarchical data – it's designed for flat data. What you're doing now – one row per computer+drive – is "flattening" the hierarchical data in the best way.

    What you've asked to do, where you'd have a column for each possible drive, would result in some computers having empty columns (what if one doesn't have a D: drive, but has an E: drive?), and is harder because you essentially have to combine objects to form an aggregated row.

    XML is actually a lot better than storing this kind of data than CSV, because XML supports hierarchies.

  • #24201

    Points: 502
    Helping Hand
    Rank: Major Contributor

    If you want to see it graphically, as a report, you can use HTML (ConvertTo-HTML) to generate a report or use an XML Stylesheet: http://www.w3schools.com/xml/xml_xsl.asp

  • #24230

    Points: 0
    Rank: Member

    Thanks a lot for your response.

The topic ‘Export-csv column format’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort