Export-csv creating multiple lines

AAP_Midrange
AAP_Midrange used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2014
Commented:
What I see is that the split is an attempt to get just part of the group's distinguishedName (unsuccessfully, because the code isn't right).  Other than that the split doesn't do anything in the above.

If you just want your output to include everything in the memberof property, then you can use the below.  Modified as little as possible so you can spot the code difference.
$results = @()
$users = Get-ADUser testuser -Properties Name,Description,Enabled,lastLogon,MemberOf
foreach($user in $users){
    $lastlogon = [datetime]::FromFileTime($user.lastlogon)
    $temp = New-Object PSCustomObject -Property @{'Name' = $user.Name;
                                            'Description' = $user.Description;
                                            'Enabled' = $user.Enabled;
                                            'LastLogon' = $lastlogon;
                                            'Group' = "$($user.memberof -join ';')";
                                            }
    $results += $temp
}
$results | Export-CSV ee-temp1.csv -NoTypeInformation

Open in new window

Author

Commented:
I think this will work but it is producing an unknown file...please see screen shot.
C--Users-u57167-Documents-FileDump-G.PNG

Author

Commented:
Please disregard the file issue, that was my fault and I have resolved it. This is exactly what I need, thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial