# Setup
Set-Location $PSScriptRoot
CLS
# Variables
$ReportOnly = $true
# Install Microsoft Graph PowerShell SDK
If (Get-InstalledModule Microsoft.Graph.Users)
{
Connect-Graph -Scopes "User.ReadWrite.All"
}
Else
{
Write-Host "Microsoft Graph PowerShell module not found - please install it first." -ForegroundColor Black -BackgroundColor Yellow
Write-Host "https://learn.microsoft.com/en-us/graph/sdks/sdk-installation#install-the-microsoft-graph-powershell-sdk"
Exit
}
# List of users to be removed (email required)
[string[]]$UsersToRemove = Get-Content -Path .\UsersToRemove.txt
# Get all guest users and show
$GuestUsers = Get-MgUser -Filter "userType eq 'Guest'"
$GuestUsers | Out-GridView -Wait -Title "Found guest users"
# Delete user if in the list
$GuestUsers | ForEach-Object {
If ($UsersToRemove.Contains($_.Mail))
{
# Output user
Write-Host "The user is listed to be removed: $($_.Mail) ($($_.Id))"
# Check if report mode
If ($ReportOnly)
{
# Warning
Write-Warning "User will not be removed - Report only mode."
}
Else
{
# Remove the user
Remove-MgUser -UserId $_.Id
}
}
}
# Disconnect
# Disconnect-Graph