Export sharepoint group and users to cvs using powershell in a particular format
-
16-02-2021 - |
Вопрос
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-PnPOnline -Url $SiteURL -UseWebLogin
#Get All Groups from Site - Exclude system Groups
$Groups = Get-PnPGroup | Where-Object {$_.OwnerTitle -ne "System Account"}
$GroupData=@()
#Get Group Details
ForEach($Group in $Groups)
{
#Get Group data
$GroupData += New-Object PSObject -Property ([ordered]@{
"Group Name" = $Group.Title
"Users" = $Group.Users.Title -join "; "
})
}
$GroupData
#Export Users data to CSV file
$GroupData | Export-Csv -NoTypeInformation $CSVFile
But what i'm after is
Groups Users
GroupA User A
GroupA User A1
GroupB UserB
GroupB User B1
Is this achievable and if yes how can i do that in powershell?
Thanks in advance
Решение
You could change it to this:
ForEach($Group in $Groups)
{
#Get Group data
foreach($user in $Group.users){
$GroupData += New-Object PSObject -Property ([ordered]@{
"Group Name" = $Group.Title
"Users" = $user.Title
})
}
}
#Export Users data to CSV file
$GroupData | Export-Csv -NoTypeInformation $CSVFile
Не связан с sharepoint.stackexchange