My client implemented SharePoint Online; One of the new feature of SharePoint Online is Users can create Group Sites( Team or Communication) Collection without any approval Process. There were more than 2000 Group Sites created by users across the organization.
Step 1: Is to get a report of how many sites are created by users by department, location details.
Step 2: is to implement some kind of governance related to who can create the Group Sites.
The following script is to extract Information regarding Group Site like Title, URL, Created By, Location and export it to CSV.
$host.Runspace.ThreadOptions = "ReuseThread"
#Definition of the function that allows to get all the members of all the Office 365 Groups in a tenant
function Get-GroupsSites
{
<#
.SYNOPSIS
Gets all the Owners who created groups and details into a CSV file.
.DESCRIPTION
The function connects to tenant with Admin credentials and loops through each Groups.
.EXAMPLE
.\Get-GroupSites -$OutDirectoryPath "C:\temp"
# the out put is found in the C:\temp\GroupSites.csv
.NOTES
Important to pass correct parameter example 'c:\temp' for the funciton to execute.
#>
param(
[string] [Parameter(Mandatory=$true)] $OutDirectoryPath
)
#Initialise all the tenant variables.
$tenantUrl = "https://sptest-admin.sharepoint.com/"
# $username = 'admin@sptest.test.com'
# $password = ConvertTo-SecureString -String 'XXXXXX' -AsPlainText -Force
$O365Cred= New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $username,$password
Connect-AzureAD -Credential $O365Cred
Connect-MsolService -Credential $O365Cred
$PSSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $O365Cred -Authentication Basic -AllowRedirection
Import-PSSession $PSSession
Write-host "Getting Groups now.."
Try
{
#Getting all the Office 365 Groups in the tenant
Write-Host "Getting all the users who created each O365 Group in the tenant ..." -foregroundcolor Green
$O365Groups = Get-UnifiedGroup
$OutPut = foreach ($O365Group in $O365Groups)
{
$usr = Get-Recipient -Identity $O365Group.ManagedBy[0]
$msg = $O365Group.DisplayName + ',' + $O365Group.SharePointSiteUrl +','+ $usr.Office +','+$usr.DisplayName +','+ $usr.PrimarySmtpAddress
# $owners Get-UnifiedGroupLinks –Identity $O365Group.Identity –LinkType "Owners"
Write-Host $msg -ForegroundColor Green
Write-host
New-Object -TypeName PSObject -Property @{
Title = $O365Group.DisplayName
SharePointSiteUrl = $O365Group.SharePointSiteUrl
Office = $usr.Office
UserName = $usr.DisplayName
UserEmail = $usr.PrimarySmtpAddress
} | Select-Object Title,SharePointSiteUrl,Office,UserName,UserEmail
}
#Write to CSV.
$OutPut | Export-CSV $OutDirectoryPath + \GroupSites.csv
}
catch [System.Exception]
{
Write-Host -ForegroundColor Red $_.Exception.ToString()
}
}
Get-GroupsSites