An App Registration is the blueprint or identity definition of an application in Microsoft Entra ID (former Azure Active Directory). When an app is registered, Azure assigns it a unique Application ID and allows you to configure permissions and authentication methods. A client secret acts like a password for the application, enabling it to authenticate itself when communicating with Azure services. Imagine your app as a person attempting to access a secure building (Azure services). The Application ID acts as their badge, identifying them, while the client secret functions as their PIN, verifying they are authorized to use the badge. Concrete examples include using App Registrations to access storage accounts, key vaults, and Cosmos DB. This post illustrates how to manually and programmatically generate new client secrets for App Registrations.
Through Azure Portal
When the client secret of an App Registration expires, there is no way to extend its expiration date, and we need to generate a new client secret. The process is straightforward in the Azure portal. Go to the App Registration (by directly searching for the app’s name in the top search bar, or through Microsoft Entra ID → App registrations in the left menu). You can find buttons to create a new client secret or delete old ones, as shown in the screenshot below. You need to be the app owner or Application Administrator in Azure AD to manage the client secrets.

Using Azure CLI
To use Azure CLI to generate a new client secret, we must get the Application ID first. Run az ad app list with --display-name or --show-mine to retrieve the Application ID of <app-registration-name>. Here we change the output format from JSON (default) to table to better locate the App ID.
az ad app list --display-name "<app-registration-name>" --output table
# If you don't know the app's name, try to list your apps by --show-mine
# Note: without --show-mine, it will display the first 100 applications in your organization
az ad app list --show-mine --output table
Optionally, if you want to check the existing client secrets of the app, run az ad app credential list:
az ad app credential list --id <app-id> --output table
We then use az ad app credential reset to generate a new client secret. Note that by default (without --append), this command clears all existing secrets and generates a new one. This makes sense because typically we only need one and the latest client secret. In our example, you can see that the two old secrets were cleared, and a new one was generated. The value of password in the returned JSON is the new client secret. This is the only chance you can copy and store the value.
az ad app credential reset --display-name "<name-for-secret>" --id <app-id>
Keep the Old Secrets
In practice, you may want to keep the old secret until the new one works in place in your services. In this case, use --append argument when you create the new secret. After the new secret is saved and functioning as expected, we can list the Key ID of the old secret and delete it with az ad app credential delete.
# Create a new client secret without deleting old ones
az ad app credential reset --display-name "<name-for-secret>" --id <app-id> --append
# List all client secrets of the app to get the Key ID of the old secret
az ad app credential list --id <app-id> --output table
# Delete the old secret by <key-id>
az ad app credential delete --id <app-id> --key-id <key-id>
Automate the Workflow
We can query the returned data of Azure CLI commands and store required information in variables to glue everything together. The code snippet below can be executed as a PowerShell or Bash script to automate the workflow.
# Generate new client secret for <app-registration-name>
$servicePrincipal = "<app-registration-name>"
$newSecretName = "<new-secret-name>"
$appId = az ad app list --display-name $servicePrincipal --query "[0].appId"
$newClientSecret = az ad app credential reset --display-name $newSecretName --id $appId --append --query "password"
# The generated secret can then be stored somewhere programmtically
echo "New client secret: $newClientSecret is generated for SP: $servicePrincipal"
# Delete the old client secret by its name
$oldSecretName = "<old-secret-name>"
$oldSecretKeyId = az ad app credential list --id $appId --query "[?displayName == '$oldSecretName'].keyId | [0]"
az ad app credential delete --id $appId --key-id $oldSecretKeyId
echo "Old secret $oldSecretName is deleted"
In $appId = az ad app list --display-name $servicePrincipal --query "[0].appId"
[0]targets the first element in the array.appIdaccesses theappIdfield in that object
In $oldSecretKeyId = az ad app credential list --id $appId --query "[?displayName == '$oldSecretName'].keyId | [0]"
?displayName == ': Filters the objects in the list to include only those where$oldSecretName'displayNameis".$oldSecretName".keyId: Extracts thekeyIdfield from the filtered objects| [0]: Retrieves the first (and only) result from the filtered list sincedisplayNamevalues are unique.
References:
