PowerShell – Office 365 – Disable an entire license package for each user

0

The purpose of this script is to remove an entire license package from every user. It is especially useful when removing trial license packages but of course not limited to them.

Every license package consists of several service plan options. You can disable them one by one with the PowerShell – Office 365 – Enable or disable service plan options within each license package while respecting current configuration for each user script. However, when it comes to the last option left, it won’t disable the whole license package. That surely makes you scratch your head and this is why this script is much needed.

Just like before, I tried to make it as interactive as possible:

  • the script is interactive – just select all actions;
  • no need to predefine any variables;
  • works with all license packages.

This script assumes you are already connected to the right services and have all modules installed.

# List License Packages
$LicensePackageChoices = @()
$i = 0
$LicensePackages = Get-MsolAccountSku
ForEach ($LicensePackage in $LicensePackages) {
    $outObject = "" | Select Number,'Package Name','PackageID'
    $i = $i + 1
    $outObject."Number" = $i
    $outObject.'Package Name' = $LicensePackage.SkuPartNumber
    $outObject.PackageID = $LicensePackage.AccountSkuId
    $LicensePackageChoices += $outObject
}
Write-host `n"Listing License Packages:" -ForegroundColor Cyan
$LicensePackageChoices | ft Number,'Package Name' | Out-Host

# Select License Package
Do {
    Try {
        $Num = $true
        [int]$SelectedInput = Read-host "By typing a number, select a License Package you would like to adjust for each user that has it"
    }
    Catch {$Num = $false}
}
Until (($SelectedInput -gt 0 -and $SelectedInput -le $LicensePackageChoices.count) -and $Num -eq $true)
$SelectedLicensePackage = $LicensePackages[[int]$SelectedInput - 1]
$SelectedLicensePackage | Out-Host

# List all licenses users with selected License Package
Write-host "Lisiting all licensed users that have $($SelectedLicensePackage.SkuPartNumber):" -ForegroundColor Cyan
$Users = Get-MsolUser -All | Where-Object {($_.isLicensed -eq $true) -and ($_.Licenses.AccountSkuId -contains $SelectedLicensePackage.AccountSkuId)}
$Users | Select-Object DisplayName,UserPrincipalName,@{Label="Currently disabled within $($SelectedLicensePackage.SkuPartNumber)";Expression={(($_.Licenses | Where-Object {$_.AccountSkuId -eq $SelectedLicensePackage.AccountSkuId}).ServiceStatus | Where-Object {$_.ProvisioningStatus -eq 'Disabled'}).ServicePlan.ServiceName -join ','}} | ft
Write-host "Total - $($Users.Count)"`n -ForegroundColor Cyan

# Disable a License Package?
$AnswerEnableOrDisableService = Read-Host "Would you like to disable $($SelectedLicensePackage.SkuPartNumber) license package for every user that has it (type 'yes' or 'no')?"
While ("yes","no" -notcontains $AnswerEnableOrDisableService) {
    $AnswerEnableOrDisableService = Read-Host "Would you like to disable $($SelectedLicensePackage.SkuPartNumber) license package for every user that has it (type 'yes' or 'no')?"
}

# If Yes, remove license packages
If ($AnswerEnableOrDisableService -eq 'yes') {
    
    # Run for each user
    foreach ($User in $Users) { 
        Set-MsolUserLicense -UserPrincipalName $User.UserPrincipalName -RemoveLicenses $SelectedLicensePackage.AccountSkuId

        # Output before and after change
        $outObject = new-object psobject -Property @{
            User = $User.DisplayName
            Disabling = $SelectedLicensePackage.AccountSkuId
        }
        $outObject | Select-Object User,Disabling
    }   
}

# If No, quit
ElseIf ($AnswerEnableOrDisableService -eq 'no') {
    Exit
}
0

Leave a Reply