Skip to content

Commit be49be0

Browse files
authored
Merge pull request pnp#899 from ojopiyo/patch-7
Create README.md
2 parents f585239 + 8914def commit be49be0

File tree

3 files changed

+222
-3
lines changed

3 files changed

+222
-3
lines changed

scripts/onedrive-export-admins/README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,103 @@ SiteCollectionAdmin
1414
SiteCollectionAdminName
1515

1616

17+
# [PnP PowerShell v2](#tab/pnppsv2)
18+
19+
## Parameters / Configuration
20+
21+
Customize the following values:
22+
- **AdminURL** – SharePoint Admin Center URL
23+
- **ClientId** – Azure AD App Client ID
24+
- **Thumbprint** – Certificate thumbprint
25+
- **Tenant** – Tenant domain (e.g., contoso.onmicrosoft.com)
26+
- **OutputFile** – Path to export the CSV
27+
28+
## Output Details
29+
30+
The CSV output file will contain the following values:
31+
- **SiteURL** – URL of the OneDrive site
32+
- **SiteName** – Name of the OneDrive site
33+
- **SiteCollectionAdmin** – Admin email address
34+
- **SiteCollectionAdminName** – Admin display name
35+
36+
37+
## Real-World Scenarios / Use Cases
38+
- Auditing OneDrive admins after an internal restructure or staff departures
39+
- Ensuring compliance and least-privilege access policies
40+
- Preparing for internal or external security audits
41+
42+
43+
## Notes / Tips
44+
- For large tenants, consider running in PowerShell 7 for better performance
45+
- CSV can be filtered in Excel to quickly identify unnecessary or excessive access
46+
- The script is **read-only**; it does not modify permissions
47+
48+
```powershell
49+
50+
# Parameters
51+
$AdminURL = "https://contoso-admin.sharepoint.com"
52+
$ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
53+
$Thumbprint = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
54+
$Tenant = "contoso.onmicrosoft.com"
55+
$OutputFile = "C:\Reports\OneDriveAdmins.csv"
56+
57+
# Connect to SharePoint Admin Center
58+
Connect-PnPOnline -Url $AdminURL -ClientId $ClientId -Thumbprint $Thumbprint -Tenant $Tenant -ErrorAction Stop
59+
60+
# Get all OneDrive (MySite) sites
61+
$MySites = Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'"
62+
63+
# Initialize results and error logs
64+
$AllAdmins = @()
65+
$ErrorLog = @()
66+
67+
# Process each MySite
68+
foreach ($MySite in $MySites) {
69+
try {
70+
Write-Host "Processing: $($MySite.Title)" -ForegroundColor Green
71+
72+
# Connect to the MySite
73+
Connect-PnPOnline -Url $MySite.Url -ClientId $ClientId -Thumbprint $Thumbprint -Tenant $Tenant -ErrorAction Stop
74+
75+
# Get site collection admins
76+
$Admins = Get-PnPSiteCollectionAdmin -ErrorAction Stop
77+
78+
foreach ($admin in $Admins) {
79+
$AllAdmins += [PSCustomObject]@{
80+
SiteURL = $MySite.Url
81+
SiteName = $MySite.Title
82+
SiteCollectionAdmin = $admin.Email
83+
SiteCollectionAdminName = $admin.Title
84+
}
85+
}
86+
}
87+
catch {
88+
Write-Warning "Error processing site $($MySite.Title): $_"
89+
$ErrorLog += [PSCustomObject]@{
90+
SiteURL = $MySite.Url
91+
SiteName = $MySite.Title
92+
ErrorMessage = $_.Exception.Message
93+
}
94+
}
95+
}
96+
97+
# Export results to CSV
98+
$AllAdmins | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8
99+
100+
# Export errors if any
101+
if ($ErrorLog.Count -gt 0) {
102+
$ErrorFile = "C:\Reports\Errors-OneDriveAdmins.csv"
103+
$ErrorLog | Export-Csv -Path $ErrorFile -NoTypeInformation -Encoding UTF8
104+
Write-Warning "Errors encountered. See $ErrorFile for details."
105+
}
106+
107+
Write-Host "Script completed successfully!" -ForegroundColor Green
108+
109+
110+
```
111+
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]
112+
113+
17114
# [PnP PowerShell](#tab/pnpps)
18115

19116
```powershell
@@ -70,6 +167,7 @@ foreach ($MySite in $MySites) {
70167
| Author(s) |
71168
|-----------|
72169
| Matt Maher |
170+
| [Josiah Opiyo](https://github.com/ojopiyo) |
73171

74172

75173
[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
2+
3+
# M365 Get OneDrive Admins Report
4+
5+
## Summary
6+
Imagine your organization recently had an internal audit, or you’re reviewing governance after a team restructure. You need to know which administrators have access to employee OneDrive accounts to ensure no unnecessary permissions exist. Get-OneDrive-Admins helps by exporting all OneDrive sites in the tenant along with their Site Collection Administrators. With this report, you can quickly identify administrators who may have elevated access they don’t need — for example, ex-managers, IT staff who no longer require direct access, or temporary admins — and take action to remove unnecessary permissions. The output is CSV-based, making it easy to filter, sort, and review in Excel.
7+
8+
9+
## Parameters / Configuration
10+
11+
Customize the following values:
12+
- **AdminURL** – SharePoint Admin Center URL
13+
- **ClientId** – Azure AD App Client ID
14+
- **Thumbprint** – Certificate thumbprint
15+
- **Tenant** – Tenant domain (e.g., contoso.onmicrosoft.com)
16+
- **OutputFile** – Path to export the CSV
17+
18+
## Output Details
19+
20+
The CSV output file will contain the following values:
21+
- **SiteURL** – URL of the OneDrive site
22+
- **SiteName** – Name of the OneDrive site
23+
- **SiteCollectionAdmin** – Admin email address
24+
- **SiteCollectionAdminName** – Admin display name
25+
26+
27+
## Real-World Scenarios / Use Cases
28+
- Auditing OneDrive admins after an internal restructure or staff departures
29+
- Ensuring compliance and least-privilege access policies
30+
- Preparing for internal or external security audits
31+
32+
33+
## Notes / Tips
34+
- For large tenants, consider running in PowerShell 7 for better performance
35+
- CSV can be filtered in Excel to quickly identify unnecessary or excessive access
36+
- The script is **read-only**; it does not modify permissions
37+
38+
# [PnP PowerShell](#tab/pnpps)
39+
40+
```powershell
41+
42+
# Parameters
43+
$AdminURL = "https://contoso-admin.sharepoint.com"
44+
$ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
45+
$Thumbprint = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
46+
$Tenant = "contoso.onmicrosoft.com"
47+
$OutputFile = "C:\Reports\OneDriveAdmins.csv"
48+
49+
# Connect to SharePoint Admin Center
50+
Connect-PnPOnline -Url $AdminURL -ClientId $ClientId -Thumbprint $Thumbprint -Tenant $Tenant -ErrorAction Stop
51+
52+
# Get all OneDrive (MySite) sites
53+
$MySites = Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'"
54+
55+
# Initialize results and error logs
56+
$AllAdmins = @()
57+
$ErrorLog = @()
58+
59+
# Process each MySite
60+
foreach ($MySite in $MySites) {
61+
try {
62+
Write-Host "Processing: $($MySite.Title)" -ForegroundColor Green
63+
64+
# Connect to the MySite
65+
Connect-PnPOnline -Url $MySite.Url -ClientId $ClientId -Thumbprint $Thumbprint -Tenant $Tenant -ErrorAction Stop
66+
67+
# Get site collection admins
68+
$Admins = Get-PnPSiteCollectionAdmin -ErrorAction Stop
69+
70+
foreach ($admin in $Admins) {
71+
$AllAdmins += [PSCustomObject]@{
72+
SiteURL = $MySite.Url
73+
SiteName = $MySite.Title
74+
SiteCollectionAdmin = $admin.Email
75+
SiteCollectionAdminName = $admin.Title
76+
}
77+
}
78+
}
79+
catch {
80+
Write-Warning "Error processing site $($MySite.Title): $_"
81+
$ErrorLog += [PSCustomObject]@{
82+
SiteURL = $MySite.Url
83+
SiteName = $MySite.Title
84+
ErrorMessage = $_.Exception.Message
85+
}
86+
}
87+
}
88+
89+
# Export results to CSV
90+
$AllAdmins | Export-Csv -Path $OutputFile -NoTypeInformation -Encoding UTF8
91+
92+
# Export errors if any
93+
if ($ErrorLog.Count -gt 0) {
94+
$ErrorFile = "C:\Reports\Errors-OneDriveAdmins.csv"
95+
$ErrorLog | Export-Csv -Path $ErrorFile -NoTypeInformation -Encoding UTF8
96+
Write-Warning "Errors encountered. See $ErrorFile for details."
97+
}
98+
99+
Write-Host "Script completed successfully!" -ForegroundColor Green
100+
101+
102+
```
103+
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]
104+
***
105+
106+
107+
## Contributors
108+
109+
| Author(s) |
110+
|-----------|
111+
| [Josiah Opiyo](https://github.com/ojopiyo) |
112+
113+
114+
[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
115+
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/onedrive-export-admins" aria-hidden="true" />

scripts/onedrive-export-admins/assets/sample.json

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
"This script exports every OneDrive in the tenant, and the site collection admins of the site. This helps audit which admins have unnecessary access to user OneDrives."
1010
],
1111
"creationDateTime": "2022-04-16",
12-
"updateDateTime": "2022-04-16",
12+
"updateDateTime": "2025-12-18",
1313
"products": [
1414
"SharePoint"
1515
],
1616
"metadata": [
1717
{
1818
"key": "PNP-POWERSHELL",
19-
"value": "1.5.0"
19+
"value": "3.1.0"
2020
}
2121
],
2222
"categories": [
@@ -31,10 +31,16 @@
3131
"type": "image",
3232
"order": 100,
3333
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/onedrive-export-admins/assets/OneDriveAdmins.png",
34-
"alt": "Preview of the sample <title>"
34+
"alt": "Preview of the sample Export OneDrive Admins"
3535
}
3636
],
3737
"authors": [
38+
{
39+
"gitHubAccount": "ojopiyo",
40+
"company": "",
41+
"pictureUrl": "https://github.com/ojopiyo.png",
42+
"name": "Josiah Opiyo"
43+
},
3844
{
3945
"gitHubAccount": "Maher256",
4046
"company": "Netwoven",

0 commit comments

Comments
 (0)