We help IT Professionals succeed at work.
Get Started

Export-csv creating multiple lines

Last Modified: 2016-01-12
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
Watch Question
Top Expert 2014
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE