Skip to content

Commit

Permalink
Merge pull request #751 from petkir/aad-inactive-guest-delete
Browse files Browse the repository at this point in the history
New Sample aad-inactive-guest-delete
  • Loading branch information
pkbullock authored Oct 7, 2024
2 parents 84f1359 + 89ee68d commit 79cff9f
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 0 deletions.
78 changes: 78 additions & 0 deletions scripts/aad-inactive-guest-delete/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
plugin: add-to-gallery
---

# Delete inactive Guest User

The script will report inactive users for x days and provides an option to delete them.


## Summary

This PowerShell script identifies and optionally deletes inactive guest users in Microsoft 365. It connects to Microsoft Graph, retrieves guest users, checks their last sign-in date, and lists those who have been inactive for a specified number of days. The script then prompts the user to confirm whether to delete these inactive users.


- Open Windows PowerShell ISE or VS Code
- Copy script below to your clipboard
- Modify the $daysInactive variable as needed.
- Run the script to identify and optionally delete inactive guest users.

[!INCLUDE [Delete Warning](../../docfx/includes/DELETE-WARN.md)]

# [Microsoft Graph PowerShell](#tab/graphps)

```powershell
#Install-Module Microsoft.Graph
# Define the number of days of inactivity
$daysInactive = 30
Connect-MgGraph -Scopes "User.Read.All", "User.ReadWrite.All","AuditLog.Read.All"
$calcDate = (Get-Date).AddDays($daysInactive * -1)
$guestUsers = Get-MgUser -Filter "userType eq 'Guest'" -All -Property id,displayName,mail,signInActivity,UserPrincipalName
$inactiveUsers = @()
foreach ($user in $guestUsers) {
if ($user.SignInActivity.LastSignInDateTime -ge $calcDate) {
$inactiveUsers += $user
}
}
if ($inactiveUsers.Count -gt 0) {
Write-Host "The following guest users have been inactive for $daysInactive days or more:"
$inactiveUsers | ForEach-Object {
Write-Host "$($_.DisplayName) ($($_.UserPrincipalName))"
}
# Ask if the user wants to delete the inactive users
$delete = Read-Host "Do you want to delete these users? (y/n)"
if ($delete -eq 'y') {
$inactiveUsers | ForEach-Object {
Remove-MgUser -UserId $_.Id -Confirm:$false
Write-Host "Deleted user: $($_.DisplayName) ($($_.UserPrincipalName))"
}
}
} else {
Write-Host "No inactive guest users found."
}
Disconnect-MgGraph
```
[!INCLUDE [More about Microsoft Graph PowerShell SDK](../../docfx/includes/MORE-GRAPHSDK.md)]


***


## Contributors

| Author(s) |
|-----------|
| [Peter Paul Kirschner](https://github.com/petkir) |


[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/aad-inactive-guest-delete" aria-hidden="true" />
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions scripts/aad-inactive-guest-delete/assets/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[
{
"name": "aad-inactive-guest-delete",
"source": "pnp",
"title": "Delete inactive Guest User",
"shortDescription": "The script will report inactive users for x days and provides an option to delete them.",
"url": "https://pnp.github.io/script-samples/aad-inactive-guest-delete/README.html",
"longDescription": [
""
],
"creationDateTime": "2024-10-02",
"updateDateTime": "2024-10-02",
"products": [
"Graph"
],
"metadata": [
{
"key": "GRAPH-POWERSHELL",
"value": "1.0.0"
},
{
"key": "POWERSHELL",
"value": "7.2.0"
}
],
"categories": [
"Security"
],
"tags": [
"Get-MgUser",
"Remove-MgUser"
],
"thumbnails": [
{
"type": "image",
"order": 100,
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/aad-inactive-guest-delete/assets/preview.png",
"alt": "Preview of the sample Delete inactive Guest User"
}
],
"authors": [
{
"gitHubAccount": "petkir",
"company": "ACP CUBIDO Digital Solutions GmbH",
"pictureUrl": "https://github.com/petkir.png",
"name": "Peter Paul Kirschner"
}
],
"references": [
{
"name": "Want to learn more about Microsoft Graph PowerShell SDK and the cmdlets",
"description": "Check out the Microsoft Graph PowerShell SDK documentation site to get started and for the reference to the cmdlets.",
"url": "https://learn.microsoft.com/graph/powershell/get-started"
}
]
}
]

0 comments on commit 79cff9f

Please sign in to comment.