Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am writing a PowerShell script to export users' information including the SharePoint Group(s) he or she belong to. Part of script show below:

$web = Get-SPWeb $webURL
$users = Get-SPUser -Web $web.Url
foreach($user in $users){
   $staffObject = New-Object PSObject -Property @{
                'UserID' = $user.LoginName
                'Groups' = $user.Groups
                'Join Date' = ""
                'Referal' = ""
            }
$resultsarray += $staffObject 
}

$resultsarray | Select UserID,@{Name="Groups";Expression={$_."Groups" -join '|'}},'Join Date','Referral' | Export-csv -path $sFileLocation -Append -NoTypeInformation

Right now the output is like:

"UserID","Groups","Join Date","Referal"
"123","Group A|Group B|Group C","",""

How can I change to:

"UserID","Groups","Join Date","Referal"
"123","Group A","",""
"123","Group B","",""
"123","Group C","",""

Many thanks.

share|improve this question

1 Answer 1

up vote 4 down vote accepted

Does this help?

$CSV = Import-Csv C:\Users\Ryan\Downloads\test.txt
$ReformattedUsers = @()
Foreach ($User In $CSV)
{
    Foreach ($Group In $User.Groups.Split('|'))
    {
        $ReformattedUsers += [PSCustomObject]@{ 'UserID' = $User.UserID; `
                                                'Groups' = $Group; `
                                                'Join Date' = $User.'Join Date'; `
                                                'Referal (sic)' = $User.Referal  }
    }
}

$ReformattedUsers | Format-Table -AutoSize

$ReformattedUsers | Export-Csv C:\Users\Ryan\Downloads\out.csv -NoTypeInformation

Output:

UserID Groups  Join Date Referal (sic)
------ ------  --------- -------------
123    Group A                        
123    Group B                        
123    Group C                        

The resulting CSV looks like:

"UserID","Groups","Join Date","Referal (sic)"
"123","Group A","",""
"123","Group B","",""
"123","Group C","",""
share|improve this answer
    
Thank you very much! –  Mark Aug 26 at 6:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.