Check Expired License

Modified on Tue, 30 Jun at 4:47 PM

# Install and import the AzureAD module if not already installed

# Install-Module -Name Az -AllowClobber -Force -Scope CurrentUser

# Import-Module Az -Force


# Connect to Office 365

Connect-AzAccount


# Get a list of all users

$users = Get-MsolUser -All


foreach ($user in $users) {

    # Check if the user is licensed

    if ($user.IsLicensed) {

        # Check if any licenses are expired

        $expiredLicenses = $user.Licenses | Where-Object { $_.Status -eq "Disabled" -and $_.EndDate -lt (Get-Date) }


        if ($expiredLicenses.Count -gt 0) {

            Write-Host "$($user.UserPrincipalName) has expired licenses:"

            foreach ($expiredLicense in $expiredLicenses) {

                Write-Host " - $($expiredLicense.AccountSkuId) (Expired on $($expiredLicense.EndDate))"

            }

        }

    }

}


Write-Host "Completed checking for expired licenses."


Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article