Remove all licenses from a given user via the MSOL module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$users = Import-Csv .\Users-to-disable.csv | |
foreach ($user in $users) { | |
Write-Verbose "Processing licenses for user $($user.UserPrincipalName)" | |
try { $user = Get-MsolUser -UserPrincipalName $user.UserPrincipalName -ErrorAction Stop } | |
catch { continue } | |
$SKUs = @($user.Licenses) | |
if (!$SKUs) { Write-Verbose "No Licenses found for user $($user.UserPrincipalName), skipping..." ; continue } | |
foreach ($SKU in $SKUs) { | |
if (($SKU.GroupsAssigningLicense.Guid -ieq $user.ObjectId.Guid) -or (!$SKU.GroupsAssigningLicense.Guid)) { | |
Write-Verbose "Removing license $($Sku.AccountSkuId) from user $($user.UserPrincipalName)" | |
Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -RemoveLicenses $SKU.AccountSkuId | |
} | |
else { | |
Write-Verbose "License $($Sku.AccountSkuId) is assigned via Group, use the Azure AD blade to remove it!" | |
continue | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment