Create Azure AD B2C custom policies and applications using powershell

Viewed 1331

I want to create custom B2C policies and B2C applications in Azure AD B2C. I have referred to the documentation here and it is working perfectly for me.

My problem is that I have to do all the steps under Identity Experience Framework, as well as the steps for creating a new application for every new tenant in which I have to use B2C features. After going through the documentation here, I found this

Applications created from the Azure AD B2C blade in the Azure portal must be managed from the same location. If you edit the B2C applications using PowerShell or another portal, they become unsupported and will not work with Azure AD B2C.

but I am not able to infer much regarding custom policies from it.

I wanted to know if I can use an automated way such as a powershell script to achieve my purpose of creating custom B2C policies as well as create B2C applications.

3 Answers

You can use PowerShell AzureADPreview 2.0 module to create applications.

Full doc is here: AzureADPreview 2 docs

I had no success to install this module to "old" PowerShell (5.x) so I gave a shot to the 'new' PowerShell 7 (Core). The only issue with PowerShell 7 and AzureAD module is that Connect-AzureAD uses a cryptographic function which is not in .NET Core, so you must import the AzureADPreview module using the -UseWindowsPowerShell option.

Here is a sample, works with PowerShell 7:

Install-Module AzureADPreview
Import-Module AzureADPreview -UseWindowsPowerShell
$tenantId = "yourb2ctenant.onmicrosoft.com"

# Note: this will interactively ask your credentials. 
#       If you want to run this unattended, use the -Credential parameter with a PSCredential object with a SecureString
Connect-AzureAD -TenantId $tenantId

# ready to go 

#list your all apps    
Get-AzureADApplication 

# examine one of you app and get ideas 
$application = Get-AzureADApplication -ObjectId af46a788-8e55-4301-b2df-xxxxxxxxx 

# create and application
$applicationName = "yourappname"
$application = New-AzureADApplication -DisplayName $applicationName -PublicClient $true etc
Related