See my blog post
NOTE: If the principal (application name) already exists then the password will not be updated.
Create only an Azure AD Application/Principal without any role grant:
.\createservicesprincipal.ps1 -subscriptionName "The Subscription Name" -applicationName "TheApplicationName" -password (ConvertTo-SecureString –String "ThePassword" -AsPlainText -Force)
Create an Azure AD Application/Principal and grants the Role on the specified existing Resource Groups (if the Resource Groups do not exists no error will be thrown, they will just be ignored):
.\createservicesprincipal.ps1 -subscriptionName "The Subscription Name" -applicationName "TheApplicationName" -password (ConvertTo-SecureString –String "ThePassword" -AsPlainText -Force) -resourceGroupNames "ResourceGroupName1","ResourceGroupName2","etc"
Create an Azure AD Application/Principal and the specified Resource Groups at the provided location, grants the Role to the Resource Groups:
.\createservicesprincipal.ps1 -subscriptionName "The Subscription Name" -applicationName "TheApplicationName" -password (ConvertTo-SecureString –String "ThePassword" -AsPlainText -Force) -resourceGroupNames "ResourceGroupName1","ResourceGroupName2","etc" -createResourceGroups -location "West Europe"
Create an Azure AD Application/Principal and the specified Resource Groups at the provided location, grants the Role to the Resource Groups. Also grants the AD groups to the Resource Groups:
.\createservicesprincipal.ps1 -subscriptionName "The Subscription Name" -applicationName "TheApplicationName" -password (ConvertTo-SecureString –String "ThePassword" -AsPlainText -Force) -resourceGroupNames "ResourceGroupName1","ResourceGroupName2","etc" -adGroupNames "AdGroupName1", "AdGroupName2", "etc" -createResourceGroups -location "West Europe"
Create an Azure AD Application/Principal and grants the Role at subscription level:
.\createservicesprincipal.ps1 -subscriptionName "The Subscription Name" -applicationName "TheApplicationName" -password (ConvertTo-SecureString –String "ThePassword" -AsPlainText -Force) -grantRoleOnSubscriptionLevel
The default value for the password expiration is 1/1/2099 1:00 AM, you can provide another value like this (in the following example I'm using the CreateVSTSPrincipalOnly paramter set, you can use it with all the calls) :
.\createservicesprincipal.ps1 -subscriptionName "The Subscription Name" -applicationName "TheApplicationName" -password (ConvertTo-SecureString –String "ThePassword" -AsPlainText -Force) -passwordExpirationDateTime (Get-Date "1/1/2020 1:00 AM")