Skip to main content

Enterprise application secrets and certificates that have expired

Which service principals (Enterprise applications) have a client secret or certificate that has already expired, or expires within the number of days you pass?

Microsoft Entra IDPowerShell 7.6 Read-only
Cmdlet lint
Passed
20 Nov 2026
Changes anything
No
Read-only
Pinned versions
3

Script

entra-service-principal-credentials-expired.ps1
#Requires -Modules Microsoft.Graph.Authentication, Microsoft.Graph.Applications
# Service principal (Enterprise application) client secrets and certificates that have expired, or expire within -Days. Read-only.
param(
    [int]$Days = 0
)

Connect-MgGraph -Scopes 'Application.Read.All' -NoWelcome

# Graph returns endDateTime in UTC, so compare against UTC.
$now = (Get-Date).ToUniversalTime()
$limit = $now.AddDays($Days)

Get-MgServicePrincipal -All -Property 'id', 'appId', 'displayName', 'servicePrincipalType', 'appOwnerOrganizationId', 'accountEnabled', 'passwordCredentials', 'keyCredentials' |
    ForEach-Object {
        $sp = $_
        $credentials = @(
            $sp.PasswordCredentials | ForEach-Object { [pscustomobject]@{ Type = 'Client secret'; Usage = $null; Credential = $_ } }
            $sp.KeyCredentials | ForEach-Object { [pscustomobject]@{ Type = 'Certificate'; Usage = $_.Usage; Credential = $_ } }
        )
        foreach ($entry in $credentials) {
            if ($entry.Credential.EndDateTime -le $limit) {
                [pscustomobject]@{
                    ServicePrincipal       = $sp.DisplayName
                    AppId                  = $sp.AppId
                    ServicePrincipalType   = $sp.ServicePrincipalType
                    AppOwnerOrganizationId = $sp.AppOwnerOrganizationId
                    AccountEnabled         = $sp.AccountEnabled
                    Type                   = $entry.Type
                    Usage                  = $entry.Usage
                    Name                   = $entry.Credential.DisplayName
                    KeyId                  = $entry.Credential.KeyId
                    EndDateTime            = $entry.Credential.EndDateTime
                    Expired                = $entry.Credential.EndDateTime -lt $now
                }
            }
        }
    } |
    Sort-Object EndDateTime

Checked automatically against Microsoft's published cmdlet reference. KBY hasn't run it in your tenant; run it in a test environment first.

Reading the output

  • Reads the passwordCredentials and keyCredentials on service principal objects (Enterprise applications). Credentials on application objects (App registrations) are not read; the app registration secrets script covers those.
  • By default only expired credentials are listed. Pass -Days 30, for example, to also list ones that expire in the next 30 days. Expired = True marks the ones already past their end date.
  • Service principals for apps registered in other tenants, Microsoft's own included, are listed too. AppOwnerOrganizationId is the tenant the app is registered in, so compare it with your tenant ID before chasing a credential you don't own. Managed identities show ServicePrincipalType = ManagedIdentity.
  • Usage is filled in for certificates only (for example Verify or Sign); passwordCredential has no usage property. Microsoft limits $select of keyCredentials on service principals to 150 requests a minute per tenant, which matters only in very large tenants or when the script runs in a tight loop.

Sources

The Microsoft reference page for every cmdlet the script calls, plus the API and product pages behind each permission and caveat.

All scripts