Power Platform Licenses Recovery for Deactivated Users with PowerShell

When managing Power Platform for organizations and as part of my administration routine, I look at cost management for the tenant and optimize license management. I might not negotiate licenses with Microsoft, but I monitor licensing usage.

Once, I noticed we were nearly consuming all available Power Apps Premium licenses, even though we had many licenses. Upon closer look and research, I discovered that their licenses are not automatically freed when we deactivate or delete users (soft delete).

I wanted to quickly see all deactivated/soft-deleted users who still have licenses assigned to them, but I couldn’t find that option in the Power Platform Admin Center. My next step was to leverage PowerShell for Power Platform and create the following PowerShell script, which produces a CSV file containing Deactivated/Deleted users who still have licenses assigned to them. Here’s the script:

# Install Microsoft Graph module (if not already installed)
Install-Module Microsoft.Graph -Scope CurrentUser

# Connect to Microsoft Graph with the necessary permissions
Connect-MgGraph -Scopes "User.Read.All", "Directory.Read.All"

# Function to check licenses for deactivated users
function Get-DeactivatedUserLicenses {
    # Create an array to store the results
    $results = @()

    # Fetch all deactivated users (accountEnabled is false)
    $DeactivatedUsers = Get-MgUser -Filter "accountEnabled eq false" -All

    if ($DeactivatedUsers.Count -eq 0) {
        Write-Host "No deactivated users found."
    } else {
        Write-Host "Deactivated Users and their licenses:"
        foreach ($user in $DeactivatedUsers) {
            # Get assigned licenses for each deactivated user
            $licenses = Get-MgUserLicenseDetail -UserId $user.Id
            if ($licenses.SkuId) {
                foreach ($license in $licenses.SkuId) {
                    $licenseDetail = Get-MgSubscribedSku | Where-Object { $_.SkuId -eq $license }
                    # Add user and license details to the results array
                    $results += [PSCustomObject]@{
                        UserPrincipalName = $user.UserPrincipalName
                        License = $licenseDetail.SkuPartNumber
                        Status = "Deactivated"
                    }
                }
            }
        }
    }

    # Return results
    return $results
}

# Function to check licenses for deleted (soft-deleted) users
function Get-DeletedUserLicenses {
    # Create an array to store the results
    $results = @()

    # Fetch all users and filter for deleted users in PowerShell
    $AllUsers = Get-MgUser -All
    $DeletedUsers = $AllUsers | Where-Object { $_.DeletedDateTime -ne $null }

    if ($DeletedUsers.Count -eq 0) {
        Write-Host "No deleted users found."
    } else {
        Write-Host "Deleted Users and their licenses:"
        foreach ($user in $DeletedUsers) {
            # Get assigned licenses for each deleted user
            $licenses = Get-MgUserLicenseDetail -UserId $user.Id
            if ($licenses.SkuId) {
                foreach ($license in $licenses.SkuId) {
                    $licenseDetail = Get-MgSubscribedSku | Where-Object { $_.SkuId -eq $license }
                    # Add user and license details to the results array
                    $results += [PSCustomObject]@{
                        UserPrincipalName = $user.UserPrincipalName
                        License = $licenseDetail.SkuPartNumber
                        Status = "Deleted"
                    }
                }
            }
        }
    }

    # Return results
    return $results
}

# Execute both functions and combine results
$deactivatedResults = Get-DeactivatedUserLicenses
$deletedResults = Get-DeletedUserLicenses
$allResults = $deactivatedResults + $deletedResults

# Filter out users with no licenses assigned
$filteredResults = $allResults | Where-Object { $_.License -ne $null }

# Export filtered results to CSV
$csvPath = "C:\OldUsersLicenses.csv"
$filteredResults | Export-Csv -Path $csvPath -NoTypeInformation

Write-Host "Results saved to $csvPath"

I got in touch with the Admins, and we were able to free up a lot of licenses. The cool thing is that they had automation to take care of this, but they need to update some criteria in their process to take care of this.

Share