Skip to content

Instantly share code, notes, and snippets.

@Mark-Broadhurst
Created May 30, 2025 12:51
Show Gist options
  • Save Mark-Broadhurst/2da97e1e4860e43d258e471f46280c7e to your computer and use it in GitHub Desktop.
Save Mark-Broadhurst/2da97e1e4860e43d258e471f46280c7e to your computer and use it in GitHub Desktop.
Remove all role assignments for user
# Login to Azure
Connect-AzAccount -SkipContextPopulation
# Get all subscriptions
$subscriptions = Get-AzSubscription
# Loop through each subscription
foreach ($subscription in $subscriptions) {
# Set the current subscription context
Set-AzContext -SubscriptionId $subscription.Id
# Get the current user's object ID
$currentUser = Get-AzADUser -UserPrincipalName "[email protected]"
# Get all role assignments for the current user
$roleAssignments = Get-AzRoleAssignment -ObjectId $currentUser.Id
# Loop through each role assignment and remove it
foreach ($roleAssignment in $roleAssignments) {
Remove-AzRoleAssignment -ObjectId $roleAssignment.ObjectId -RoleDefinitionName $roleAssignment.RoleDefinitionName -Scope $roleAssignment.Scope
Write-Output "Removed role assignment: $($roleAssignment.RoleDefinitionName) in subscription: $($subscription.Name)"
}
}
Write-Output "All role assignments removed across all subscriptions."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment