Created the PowerShell script for the bulk reset redemption for guest users in Azure tenant.
The guest users list for the domain is fetched and saved into a CSV file (BeforeReset).
The script asks how many users you want to process in the first subset and proceeds only with that subset.
After processing the subset, it prompts you to confirm if you want to proceed with the remaining users.
Logs for success and errors are maintained for both subsets.
The -SendInvitationMessage parameter has been removed to prevent sending the invitation emails.
================================================================
Import required module and authenticate
#Install-Module Microsoft.Graph -Force
Connect to Microsoft Graph
Connect-MgGraph -TenantId "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" -Scopes "User.ReadWrite.All"
Export user list before reset
$guestUsers = Get-MgUser -Filter "userType eq 'Guest' and endsWith(mail, '@contoso.com')" -ConsistencyLevel eventual -CountVariable RecordCount -All
Exporting the list of guest users to a CSV file
$exportPath = "C:\temp\GuestUsersBeforeReset.csv"
$guestUsers | Select-Object DisplayName, Mail, UserPrincipalName, Id | Export-Csv -Path $exportPath -NoTypeInformation
Write-Host "Exported guest user list to $exportPath. Total records: $RecordCount"
Ask how many users to process in the first subset
$subsetCount = int
$subsetUsers = $guestUsers | Select-Object -First $subsetCount
Confirm before processing the subset
$proceedSubset = Read-Host "Do you want to proceed with resetting redemption for the first $subsetCount users? (yes/no)"
if ($proceedSubset -ne "yes") {
Write-Host "Operation cancelled by user."
exit
}
Logs for success and errors
$successLog = "C:\temp\ResetRedemptionSuccess.csv"
$errorLog = "C:\temp\ResetRedemptionErrors.csv"
Reset redemption for the subset of users
foreach ($user in $subsetUsers) {
try {
New-MgInvitation `
-InvitedUserEmailAddress $user.Mail `
-InviteRedirectUrl "https://myapps.microsoft.com" `
-ResetRedemption `
-InvitedUser $user
# Log success
Add-Content -Path $successLog -Value "$($user.DisplayName),$($user.Mail),Success"
Write-Host "Redemption reset successfully for: $($user.Mail)" -ForegroundColor Green
} catch {
# Log errors
Add-Content -Path $errorLog -Value "$($user.DisplayName),$($user.Mail),Error: $_"
Write-Host "Error resetting redemption for: $($user.Mail)" -ForegroundColor Red
}
}
Ask if user wants to proceed with the remaining users
$remainingUsers = $guestUsers | Select-Object -Skip $subsetCount
$remainingCount = $remainingUsers.Count
$proceedAll = Read-Host "Do you want to proceed with resetting redemption for the remaining $remainingCount users? (yes/no)"
if ($proceedAll -ne "yes") {
Write-Host "Operation for remaining users cancelled by user."
exit
}
Reset redemption for remaining users
foreach ($user in $remainingUsers) {
try {
New-MgInvitation `
-InvitedUserEmailAddress $user.Mail `
-InviteRedirectUrl "https://myapps.microsoft.com" `
-ResetRedemption `
-InvitedUser $user
# Log success
Add-Content -Path $successLog -Value "$($user.DisplayName),$($user.Mail),Success"
Write-Host "Redemption reset successfully for: $($user.Mail)" -ForegroundColor Green
} catch {
# Log errors
Add-Content -Path $errorLog -Value "$($user.DisplayName),$($user.Mail),Error: $_"
Write-Host "Error resetting redemption for: $($user.Mail)" -ForegroundColor Red
}
}
Write-Host "Redemption reset process completed. Success log: $successLog, Error log: $errorLog"
Export user list after the reset
$guestUsersAfterReset = Get-MgUser -Filter "userType eq 'Guest' and endsWith(mail, '@contoso.com')" -ConsistencyLevel eventual -CountVariable RecordCountAfter -All
$exportAfterPath = "C:\temp\GuestUsersAfterReset.csv"
$guestUsersAfterReset | Select-Object DisplayName, Mail, UserPrincipalName, Id | Export-Csv -Path $exportAfterPath -NoTypeInformation
Write-Host "Exported guest user list after reset to $exportAfterPath. Total records: $RecordCountAfter"