Output Data to CSV

后端 未结 1 1865
谎友^
谎友^ 2021-01-27 03:47

Can someone please tell me how I can output data in a CSV format? I want to have it so that it clearly shows what users belong to what group:



        
相关标签:
1条回答
  • 2021-01-27 04:38

    For exporting data to CSV use Export-Csv. Note, however, that Export-Csv exports the properties of objects as the CSV fields, so you have to prepare your data accordingly.

    $groups | ForEach-object {
      $grp = $_
      $grp.Users | ForEach-Object {
        New-Object -Type PSObject -Property @{
          'User'  = $_.Name
          'Group' = $grp.Name
        }
      }
    } | Export-Csv 'C:\path\to\output.csv' -NoType
    

    If you want one line per group with all the users in a single field, you could do something like this:

    $groups | ForEach-Object {
      New-Object -Type PSObject -Property @{
        'Group' = $_.Name
        'Users' = $_.Users -join '/'
      }
    } | Export-Csv 'C:\path\to\output.csv' -NoType
    
    0 讨论(0)
提交回复
热议问题