I found the following Powershell script. The script works only the export to CSV changes the order of the data. In the CSV the user is first and then the other values come as in the script. I no longer have any idea how to change it. Maybe someone of you has an idea.
$AdminCenterURL = "https://contoso-admin.sharepoint.com/"
$CSVPath = "C:\Temp\sharepoint.csv"
Connect-SPOService -url $AdminCenterURL
$GroupsData = @()
Get-SPOSite -Limit ALL | ForEach-Object {
Write-Host -f Yellow "Processing Site Collection:"$_.URL
$SiteGroups = Get-SPOSiteGroup -Site $_.URL
Write-host "Total Number of Groups Found:"$SiteGroups.Count
ForEach($Group in $SiteGroups)
{
$GroupsData = New-Object PSObject -Property @{
'Site URL' = $_.URL
'Group Name' = $Group.Title
'Permissions' = $Group.Roles -join ","
'Users' = $Group.Users -join ","
}
}
}
$GroupsData | Export-Csv $CSVPath -NoTypeInformation -encoding utf8
Write-host -f Green "Groups Report Generated Successfully!"
The export should be in the following order:
Site Url, Group Name, Permissions and finally all users
CodePudding user response:
The order in which the properties appear in your code is random, because of the way you construct the object output: a new object which takes its properties from a Hashtable.
(In a Hashtable @{}
, properties have no fixed order)
You can easily change that to produce the order you need by outputting PsCustomObjects
instead.
Below, also captures the output in variable $GroupsData
straight away, which is a much better approach than using =
addition on an array.
$GroupsData = Get-SPOSite -Limit ALL | ForEach-Object {
Write-Host "Processing Site Collection: $($_.URL)" -ForegroundColor Yellow
$SiteGroups = Get-SPOSiteGroup -Site $_.URL
Write-host "Total Number of Groups Found: $($SiteGroups.Count)"
foreach($Group in $SiteGroups) {
[PsCustomObject]@{
'Site URL' = $_.URL
'Group Name' = $Group.Title
'Permissions' = $Group.Roles -join ","
'Users' = $Group.Users -join ","
}
}
}
$GroupsData | Export-Csv $CSVPath -NoTypeInformation -Encoding utf8
The same can also be achieved using Select-Object:
$GroupsData = Get-SPOSite -Limit ALL | ForEach-Object {
# store this url in a variable first
$siteUrl = $_.URL
Write-Host "Processing Site Collection: $siteUrl" -ForegroundColor Yellow
$SiteGroups = Get-SPOSiteGroup -Site $siteUrl
Write-host "Total Number of Groups Found: $($SiteGroups.Count)"
foreach ($Group in $SiteGroups) {
$Group | Select-Object @{Name = 'Site URL'; Expression = {$siteUrl}},
@{Name = 'Group Name'; Expression = {$_.Title}},
@{Name = 'Permissions'; Expression = {$_.Roles -join ","}},
@{Name = 'Users'; Expression = {$_.Users -join ","}}
}
}
$GroupsData | Export-Csv $CSVPath -NoTypeInformation -Encoding utf8