-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRemove-SharePointOnlineOrphanedUsers.ps1
57 lines (52 loc) · 2.42 KB
/
Remove-SharePointOnlineOrphanedUsers.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#========================================================================
# Created on: a long time ago....
# Created by: Andreas Hähnel
# Organization: Black Magic Cloud
# Script Version: 1.0
#========================================================================
# RequiredPermissions and modules:
# Delegated (work or school account) Not supported.
# Delegated (personal Microsoft account) Not supported.
# Application Sites.FullControl.All
# User.Read.All
#========================================================================
# Description:
# this script identifies orphaned users in SPO and removes them.
#========================================================================
#Requires -Version 3
#Requires -Modules AzureAD
#Requires -Modules PnP.PowerShell
param(
[Parameter(Mandatory)][string]$thumbprint,
[Parameter(Mandatory)][string]$appID,
[Parameter(Mandatory)][string]$tenantID,
[Parameter(Mandatory)][string]$tenantURL,
[Parameter(Mandatory)][string]$tenant
)
# connect to aad
Connect-AzureAD -CertificateThumbprint $thumbprint -ApplicationId $appId -TenantId $tenantId
# get all aad user entities
$allAADUsers = Get-AzureADUser -All:$true
$allAADUserUPNs = @()
$allAADUsers | ForEach-Object {$allAADUserUPNs += $_.UserPrincipalName}
# connect to spo admin site to get all sites in tenant
$pnpAdminConnection = Connect-PnPOnline -Url $tenantURL -ClientId $appId -Thumbprint $thumbprint -Tenant $tenant -ReturnConnection
$allTenantSites = Get-PnPTenantSite -IncludeOneDriveSites -Connection $pnpAdminConnection
# iterate through all sites and do magic
$allTenantSites | ForEach-Object {
$pnpSiteConnection = Connect-PnPOnline -Url $_.Url -ClientId $appId -Thumbprint $thumbprint -Tenant $tenant -ReturnConnection
Write-Host "connected to $($_.Url)"
$allSiteEntities = Get-PnPUser -Connection $pnpSiteConnection
# filter for user entities
$allSiteUsers = $allSiteEntities | Where-Object {$_.LoginName -like "i:0#.f|*"}
$allSiteUsers | ForEach-Object {
# filter anonymous links
if ($_.LoginName -notlike "*urn%3aspo%3aanon#*") {
if ($_.Email -notin $allAADUserUPNs) {
# remove if not present in aad
Remove-PnPUser -Identity $_.Id -Force -Connection $pnpSiteConnection
Write-Host "Removed orphaned user $($_.LoginName)"
}
}
}
}