Skip to content

Commit eac8971

Browse files
committed
Split into two versions as mentioned
1 parent 164f128 commit eac8971

1 file changed

Lines changed: 94 additions & 1 deletion

File tree

  • scripts/get-disabled-or-inactive-user-accounts

scripts/get-disabled-or-inactive-user-accounts/README.md

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ The purpose of this script is to support Microsoft 365 governance by identifying
2121
- Supporting periodic access reviews and governance audits
2222
- Preparing for offboarding or tenant cleanup initiatives
2323

24-
# [PnP PowerShell](#tab/pnpps)
24+
# [PnP PowerShell V2](#tab/pnppsv2)
2525

2626
```powershell
2727
@@ -150,6 +150,99 @@ $results |
150150
151151
```
152152
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]
153+
154+
# [PnP PowerShell](#tab/pnpps)
155+
156+
In order to keep your tenant clean (Governance), you might want to ensure that disabled or inactive user accounts will be replaced where oppropriate (Think Owners of sites/groups, assignedto user on tasks/planner and so on). This script will help you find those accounts.
157+
158+
```powershell
159+
160+
function Get-UserFromGraph
161+
{
162+
$disabledusersfromgraph = @()
163+
$result = Invoke-PnPGraphMethod -Url "users?`$select=displayName,mail, AccountEnabled" -Connection $conn
164+
165+
$result.value.Count
166+
foreach($account in $result.value)
167+
{
168+
if($account.accountEnabled -eq $false)
169+
{
170+
$disabledusersfromgraph += $account.mail
171+
}
172+
}
173+
$disabledusersfromgraph
174+
}
175+
function Get-UserFromSharePointSearch
176+
{
177+
$usersfromsearch = @()
178+
#How you tag an account as disabled varies from org to org, so you might need to change the below
179+
#in one tenant the account name was prefixed with ZZ_[Year of leaving]
180+
#in another tenant they had a custom property called EmployeeStatus, and sometimes a DateLeft property
181+
#SourceId "b09a7990-05ea-4af9-81ef-edfab16c4e31" is the People source in SharePoint
182+
$results = Invoke-PnPSearchQuery -Query "*" -SourceId "b09a7990-05ea-4af9-81ef-edfab16c4e31" -All -Connection $conn
183+
184+
foreach($result in $results.ResultRows)
185+
{
186+
#you can replace this with whatever you use to tag an account as disabled
187+
if($result["SPS-HideFromAddressLists"] -eq $true)
188+
{
189+
$usersfromsearch += $result["WorkEmail"]
190+
}
191+
}
192+
$usersfromsearch
193+
}
194+
function Get-UserFromGraphThatHasntLoggedInResently($duration = 90)
195+
{
196+
$inactiveusersfromgraph = @()
197+
$authToken = Get-PnPGraphAccessToken -Connection $conn
198+
$uri = "https://graph.microsoft.com/v1.0/users"
199+
$Headers = @{
200+
"Authorization" = "Bearer $($authToken)"
201+
"Content-type" = "application/json"
202+
}
203+
$response = Invoke-RestMethod -Headers $Headers -Uri $uri -Method GET
204+
foreach($user in $response.value)
205+
{
206+
# requires the AuditLog.Read.All permission
207+
$signinsUri = "https://graph.microsoft.com/v1.0/auditLogs/signIns?$top=1&$filter=userPrincipalName eq '$($user.userPrincipalName)')"
208+
$response = Invoke-RestMethod -Headers $Headers -Uri $signinsUri -Method GET
209+
210+
if($response.value.Count -eq 0)
211+
{
212+
#no signin found
213+
$inactiveusersfromgraph += $user.userPrincipalName
214+
}
215+
else {
216+
if($response.value[0].createdDateTime -lt (Get-Date).AddDays(-$duration))
217+
{
218+
#user has not signed in for 90 days
219+
$inactiveusersfromgraph += $user.userPrincipalName
220+
221+
}
222+
}
223+
}
224+
$inactiveusersfromgraph
225+
}
226+
227+
228+
$ClientId = "clientid"
229+
$TenantName = "[domain].onmicrosoft.com"
230+
$SharePointAdminSiteURL = "https://[domain]-admin.sharepoint.com/"
231+
#connect to SharePoint using a certificate or similar
232+
$conn = Connect-PnPOnline -Url $SharePointAdminSiteURL -ClientId $ClientId -Tenant $TenantName -CertificatePath "C:\Users\[you]\[CertName].pfx" -CertificatePassword (ConvertTo-SecureString -AsPlainText -Force "ThePassWord") -ReturnConnection
233+
234+
#get user data from graph and log those which are disabled
235+
$userd1 = Get-UserFromGraph
236+
$userd2 = Get-UserFromSharePointSearch
237+
$users3 = Get-UserFromGraphThatHasntLoggedInResently
238+
239+
#output to csv file or use the data in some other way, like checking if the disabled users is a Owner of some site or group
240+
$userd1 | Export-Csv -Path "C:\temp\disabledusers.csv" -NoTypeInformation
241+
242+
```
243+
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]
244+
245+
153246
***
154247

155248

0 commit comments

Comments
 (0)