Forum Discussion
Patrick Rote
Oct 12, 2020Iron Contributor
Export sharepoint group and users to cvs using poweshell in a particular format
Hi All, I have been able to export Groups and users in this format Groups Users GroupA User A; User A1 GroupB UserB; User B1 Using this code below #Connect to PnP Online
Connect...
AndySvints
Oct 13, 2020Iron Contributor
Hello Patrick Rote,
Yes it is achievable. One of the options would be to add one more loop for users.
Also I would strongly recommend not to use += as if you have many groups/users it will take forever to complete. Please use List(System.Collections.Generic.List) instead.
Here is adjusted code:
Connect-PnPOnline -Url $SiteURL -UseWebLogin
$Groups = Get-PnPGroup | Where-Object {$_.OwnerTitle -ne "System Account"}
#Creating List object instead of Array
$GroupData=[System.Collections.Generic.List[object]]::new()
#Loop to enumerate all Groups
ForEach($Group in $Groups)
{
#Loop to enumerate all Users
foreach($User in $Group.Users.Title){
#Get Group & Add to list
$GroupData.Add($(New-Object PSObject -Property ([ordered]@{"GroupName" = $Group.Title;"User" = $User })))
}
}
$GroupData
Hope that helps.
- naijacoderOct 16, 2020Copper Contributor
AndySvints thanks for the response. It worked like a charm