Connect to Azure AD
Azure AD PowerShell Modunle
You need install MSOnline module only once. In the next time, run to connect to Azure AD PowerShell.
##Connect to MSOnline.
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine -Force;
Install-PackageProvider -Name NuGet -Force;
Set-PSRepository -Name "PSGallery" -InstallationPolicy Trusted;
Install-Module -Name MSOnline;
Connect-MsolService;
Remove all licenses of single user
$useraccount = 'chris@leoguides.info'
(get-MsolUser -UserPrincipalName $useraccount).licenses.AccountSkuId |
foreach{ Set-MsolUserLicense -UserPrincipalName $useraccount -RemoveLicenses $_}
Remove all Office 365 licenses for a group of users from CSV file
$users = Import-Csv 'C:\Temp\Users.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
}
}
}
5/5 - (1 vote)