Avatar of AAP_Midrange
AAP_Midrange
Flag for United States of America

asked on 

Export-csv creating multiple lines

So I have found this script that is exactly what I need to export lastlogon and memberof to a .csv file. The problem is the output lists the group memberships on multiple lines instead of just one -

I believe the answer is using (join “,”) instead of (Split ('.')) but I can seem to find where to make the edit to obtain the correct results.

Any help would be greatly appreciated, thank you.

$results = @()
$users = Get-ADUser u11111 -Properties Name,Description,Enabled,lastLogon,MemberOf
foreach($user in $users){
    $lastlogon = [datetime]::FromFileTime($user.lastlogon)
    foreach($group in $user.MemberOf){
        $temp = New-Object PSCustomObject -Property @{'Name' = $user.Name;
                                                      'Description' = $user.Description;
                                                      'Enabled' = $user.Enabled;
                                                      'LastLogon' = $lastlogon;
                                                      'Group' = '';
        }
        $temp.Group = $group.Split(',')
        $results += $temp
    }
}
$results | Export-CSV C:\Output\ADDumpu57167.csv -NoTypeInformation
PowershellActive Directory

Avatar of undefined
Last Comment
AAP_Midrange

8/22/2022 - Mon