Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 90 additions & 1 deletion scripts/spo-tenant-site-inventory/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,93 @@

This script provides you the list of active sites in your tenant with their administrator and usage in MB.

## Why It Matters / Real-World Scenario

In many Microsoft 365 tenants, SharePoint sites can quickly grow in number, with varying levels of administrative access and external sharing. Without visibility, it’s easy for unused or inactive sites to remain accessible, or for administrators to have unnecessary access to sensitive content.

For example, an organization may have hundreds of departmental and project sites, some of which have never been used or have files shared externally. Security and compliance teams need to know:
- Which sites have active administrators.
- Which sites have sensitive data shared externally.
- How recently sites have been used.
- The age of sites for lifecycle and archiving decisions.

This script addresses these challenges by providing a single, comprehensive report. It allows administrators to identify inactive or outdated sites, review external sharing risks, and ensure that administrative permissions are appropriate. The insights generated help enforce least-privilege access, improve governance, and support compliance audits across the tenant.

# [PnP PowerShell V2](#tab/pnppsV2)

```powershell

param(
[Parameter(Mandatory=$true)]
[string]$AdminURL,

[Parameter(Mandatory=$true)]
[string]$OutputFile,

[switch]$IncludeLastActivity,

[switch]$IncludeExternalSharing
)

# Connect once to Admin Center
Connect-PnPOnline -Url $AdminURL -Interactive

# Get all SharePoint sites once
$sites = Get-PnPTenantSite -IncludeOneDriveSites -Detailed
$results = @()

foreach ($site in $sites) {
try {
Write-Host "Processing site: $($site.Url)" -ForegroundColor Green

# Get site collection admins directly from the tenant-wide site object
$admins = $site.Owner | ForEach-Object { $_.Title }

# Storage usage from tenant site object
$storageSize = $site.StorageUsageCurrent
$createdDate = $site.CreationDate

$lastActivity = $null
$externalSharing = $null

if ($IncludeLastActivity) {
# Connect to site once to get last modified item
Connect-PnPOnline -Url $site.Url -Interactive
$lastItem = Get-PnPListItem -List "Documents" -PageSize 1 -SortField "Modified" -SortOrder Descending -ErrorAction SilentlyContinue
if ($lastItem) {
$lastActivity = $lastItem.FieldValues.Modified
}
}

if ($IncludeExternalSharing) {
Connect-PnPOnline -Url $site.Url -Interactive
$externalFiles = Get-PnPSharingForSite -Detailed | Where-Object { $_.SharedWithUsers -match "External" }
$externalSharing = if ($externalFiles) { "Yes" } else { "No" }
}

$siteInfo = [PSCustomObject]@{
SiteUrl = $site.Url
SiteName = $site.Title
Administrators = $admins -join ";"
StorageSizeMB = $storageSize
CreatedDate = $createdDate
LastActivity = $lastActivity
ExternalSharing = $externalSharing
}

$results += $siteInfo
}
catch {
Write-Host "Error processing site $($site.Url): $_" -ForegroundColor Red
}
}

# Export once
$results | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8
Disconnect-PnPOnline
Write-Host "Site inventory exported to $OutputFile" -ForegroundColor Green
```
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]

# [PnP PowerShell](#tab/pnpps)

Expand Down Expand Up @@ -51,13 +138,15 @@ Disconnect-PnPOnline
```

[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]
***

## Contributors

| Author(s) |
|-----------|
| [Diksha Bhura](https://github.com/Diksha-Bhura) |
| [Josiah Opiyo](https://github.com/ojopiyo) |


[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-tenant-site-inventory" aria-hidden="true" />
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-tenant-site-inventory" aria-hidden="true" />
10 changes: 8 additions & 2 deletions scripts/spo-tenant-site-inventory/assets/sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
""
],
"creationDateTime": "2023-07-13",
"updateDateTime": "2023-07-13",
"updateDateTime": "2025-12-18",
"products": [
"SharePoint"
],
"metadata": [
{
"key": "PNP-POWERSHELL",
"value": "1.11.0"
"value": "3.1.0"
}
],
"categories": [
Expand All @@ -37,6 +37,12 @@
}
],
"authors": [
{
"gitHubAccount": "ojopiyo",
"company": "",
"pictureUrl": "https://github.com/ojopiyo.png",
"name": "Josiah Opiyo"
},
{
"gitHubAccount": "Diksha-Bhura",
"company": "",
Expand Down