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:
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