Home
%3CLINGO-SUB%20id%3D%22lingo-sub-811570%22%20slang%3D%22en-US%22%3EPowerShell%20Basics%3A%20How%20to%20Create%20an%20Azure%20AD%20App%20Registration%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-811570%22%20slang%3D%22en-US%22%3E%3CP%3EAzure%20Active%20Directory%20(Azure%20AD)%20is%20Microsoft's%3CSPAN%3E%26nbsp%3Bfully%20managed%20multi-tenant%20identity%20and%20access%20capabilities%20for%20app%20service.%20More%20organizations%20are%20now%20harnessing%20the%20security%20capabilities%20of%20Azure%20AD%20into%20the%20apps%20they%20create%20for%20an%20additional%20layer%20of%20authentication.%20This%20post%20will%20cover%20how%20to%20register%20an%20app%20to%20Azure%20AD%20via%20PowerShell%20to%20take%20advantage%20of%20this.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EPrerequisite%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3EThe%20%3CA%20title%3D%22Azure%20AD%20module%20Installation%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fazure%2Factive-directory%2Fad-pshell-v2-version-history%3Fhttps%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fpowershell%2Fazure%2Factive-directory%2Foverview%3FWT.mc_id%3DITOPSTALK-blog-abartolo%26amp%3Bview%3Dazureadps-2.0%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3E%3CSTRONG%3EAzure%20AD%20Module%3C%2FSTRONG%3E%3C%2FA%3Eneeds%20to%20be%20added%20to%20PowerShell%20prior%20to%20getting%20started.%20Execute%20the%20command%20below%20in%20PowerShell%20using%20elevated%20or%20Administrative%20status%3A%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EInstall-Module%20AzureAD%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EOnce%20the%20Azure%20AD%20Module%20is%20installed%2C%20run%20the%20following%20command%20in%20the%20same%20PowerShell%20window%20to%20connect%20to%20the%20required%20Azure%20AD%20tenant%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EConnect-AzureAD%3C%2FPRE%3E%0A%3CP%3E%3CSTRONG%3ENOTE%3A%3C%2FSTRONG%3EThe%20required%20TenantId%20will%20be%20required%20in%20subscriptions%20with%20multiple%20tenants.%20The%20TenantId%20value%20can%20be%20found%20in%20the%20Azure%20Portal%20navigating%20to%20Azure%20Active%20Directory%20%26gt%3B%20Properties%20and%20is%20listed%20under%20Directory%20ID.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F127539i1CA2A227707800BF%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22AzureAD_App_Registration_TenantId_001.png%22%20title%3D%22AzureAD_App_Registration_TenantId_001.png%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EAzure%20Active%20Directory%20TenantId%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERun%20the%20following%20command%20in%20the%20same%20PowerShell%20window%20to%20connect%20to%20the%20specific%20Azure%20AD%20TenantId%20(if%20required)%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%3EConnect-AzureAD%20-TenantId%20*Insert%20Directory%20ID%20here*%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EStep%201%3A%20Creating%20the%20Azure%20AD%20App%20Registration%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ENext%20the%20following%20cmdlet%20is%20run%2C%20now%20that%20required%20Azure%20AD%20tenant%20is%20connected%20to%20PowerShell%2C%20to%20capture%20the%20name%20of%20the%20application%20and%20the%20IdentifierURI.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22brush%3A%20ps%3B%22%3E%24appName%20%3D%20%22TailwindTradersSalesApp%22%0A%24appURI%20%3D%20%22https%3A%2F%2Ftailwindtraderssalesapp.twtmitt.onmicrosoft.com%22%0A%24appHomePageUrl%20%3D%20%22%3CA%20href%3D%22http%3A%2F%2Fwww.tailwindtraders.com%2F%22%20target%3D%22_blank%22%20rel%3D%22noopener%20nofollow%20noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttp%3A%2F%2Fwww.tailwindtraders.com%2F%3C%2FA%3E%22%0A%24appReplyURLs%20%3D%20%40(%24appURI%2C%20%24appHomePageURL%2C%20%22https%3A%2F%2Flocalhost%3A1234%22)%0Aif(!(%24myApp%20%3D%20Get-AzureADApplication%20-Filter%20%22DisplayName%20eq%20'%24(%24appName)'%22%20%20-ErrorAction%20SilentlyContinue))%0A%7B%0A%20%20%20%20%24myApp%20%3D%20New-AzureADApplication%20-DisplayName%20%24appName%20-IdentifierUris%20%24appURI%20-Homepage%20%24appHomePageUrl%20-ReplyUrls%20%24appReplyURLs%20%20%20%20%0A%7D%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3EStep%202%3A%20Adding%20the%20App%20Key%3C%2FSTRONG%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EWith%20the%20required%20URIs%20now%20captured%2C%20it%20is%20time%20to%20add%20the%20application%20key.%26nbsp%3B%20The%20key%20will%20be%20stored%20in%20the%20%3CSTRONG%3E%3CA%20title%3D%22Azure%20Key%20Vault%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Fkey-vault%2Fkey-vault-whatis%3FWT.mc_id%3DITOPSTALK-blog-abartolo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EAzure%20Key%20Vault%3C%2FA%3E%3C%2FSTRONG%3Ewhich%20ensures%20the%20it's%20security%20and%20disallows%20unauthorized%20access.%20Run%20the%20following%20command%20to%20invoke%20this%20process%3A%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22brush%3A%20ps%3B%22%3E%24Guid%20%3D%20New-Guid%0A%24startDate%20%3D%20Get-Date%0A%20%20%20%20%0A%24PasswordCredential%20%3D%20New-Object%20-TypeName%20Microsoft.Open.AzureAD.Model.PasswordCredential%0A%24PasswordCredential.StartDate%20%3D%20%24startDate%0A%24PasswordCredential.EndDate%20%3D%20%24startDate.AddYears(1)%0A%24PasswordCredential.KeyId%20%3D%20%24Guid%0A%24PasswordCredential.Value%20%3D%20(%5BSystem.Convert%5D%3A%3AToBase64String(%5BSystem.Text.Encoding%5D%3A%3AUTF8.GetBytes((%24Guid))))%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3ENOTE%3A%3C%2FSTRONG%3EThe%20PasswordCredential%20value%20is%20created%20as%20a%20Base64%20value%20and%20is%20saved%20in%20the%20Azure%20Key%20Vault.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSTRONG%3E%3CA%20title%3D%22How%20to%20Create%20an%20Azure%20AD%20Application%20Registration%20via%20the%20Azure%20Portal%22%20href%3D%22https%3A%2F%2Fdocs.microsoft.com%2Fen-us%2Fazure%2Factive-directory%2Fdevelop%2Fhowto-create-service-principal-portal%3FWT.mc_id%3DITOPSTALK-blog-abartolo%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noopener%20noreferrer%20noopener%20noreferrer%22%3EThis%20process%20can%20also%20be%20completed%20via%20the%20Azure%20Portal%3C%2FA%3E%3C%2FSTRONG%3Ebut%20will%20take%20much%20more%20time%20to%20complete.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-TEASER%20id%3D%22lingo-teaser-811570%22%20slang%3D%22en-US%22%3E%3CP%3EAzure%20Active%20Directory%20(Azure%20AD)%20is%20Microsoft's%3CSPAN%3E%26nbsp%3Bfully%20managed%20multi-tenant%20identity%20and%20access%20capabilities%20for%20app%20service.%20More%20organizations%20are%20now%20harnessing%20the%20security%20capabilities%20of%20Azure%20AD%20into%20the%20apps%20they%20create%20for%20an%20additional%20layer%20of%20authentication.%20This%20post%20will%20cover%20how%20to%20register%20an%20app%20to%20Azure%20AD%20via%20PowerShell%26nbsp%3Bto%20take%20advantage%20of%20this.%3C%2FSPAN%3E%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3CSPAN%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20999px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F59633i420FEE9A9109E151%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22PowerShell_Basics.jpg%22%20title%3D%22PowerShell_Basics.jpg%22%20%2F%3E%3CSPAN%20class%3D%22lia-inline-image-caption%22%20onclick%3D%22event.preventDefault()%3B%22%3EPowerShell%20Basics%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-TEASER%3E%3CLINGO-LABS%20id%3D%22lingo-labs-811570%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAnthony%20Bartolo%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EPowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E
Microsoft

Azure Active Directory (Azure AD) is Microsoft's fully managed multi-tenant identity and access capabilities for app service. More organizations are now harnessing the security capabilities of Azure AD into the apps they create for an additional layer of authentication. This post will cover how to register an app to Azure AD via PowerShell to take advantage of this.

 

Prerequisite

 

The Azure AD Module needs to be added to PowerShell prior to getting started. Execute the command below in PowerShell using elevated or Administrative status: 

 

Install-Module AzureAD

 

Once the Azure AD Module is installed, run the following command in the same PowerShell window to connect to the required Azure AD tenant: 

 

Connect-AzureAD

NOTE: The required TenantId will be required in subscriptions with multiple tenants. The TenantId value can be found in the Azure Portal navigating to Azure Active Directory > Properties and is listed under Directory ID.

 

AzureAD_App_Registration_TenantId_001.pngAzure Active Directory TenantId

 

Run the following command in the same PowerShell window to connect to the specific Azure AD TenantId (if required): 

 

Connect-AzureAD -TenantId *Insert Directory ID here*

 

Step 1: Creating the Azure AD App Registration

 

Next the following cmdlet is run, now that required Azure AD tenant is connected to PowerShell, to capture the name of the application and the IdentifierURI.

 

$appName = "TailwindTradersSalesApp"
$appURI = "https://tailwindtraderssalesapp.twtmitt.onmicrosoft.com"
$appHomePageUrl = "http://www.tailwindtraders.com/"
$appReplyURLs = @($appURI, $appHomePageURL, "https://localhost:1234")
if(!($myApp = Get-AzureADApplication -Filter "DisplayName eq '$($appName)'"  -ErrorAction SilentlyContinue))
{
    $myApp = New-AzureADApplication -DisplayName $appName -IdentifierUris $appURI -Homepage $appHomePageUrl -ReplyUrls $appReplyURLs    
}

 

Step 2: Adding the App Key

 

With the required URIs now captured, it is time to add the application key.  The key will be stored in the Azure Key Vault which ensures the it's security and disallows unauthorized access. Run the following command to invoke this process: 

 

$Guid = New-Guid
$startDate = Get-Date
    
$PasswordCredential = New-Object -TypeName Microsoft.Open.AzureAD.Model.PasswordCredential
$PasswordCredential.StartDate = $startDate
$PasswordCredential.EndDate = $startDate.AddYears(1)
$PasswordCredential.KeyId = $Guid
$PasswordCredential.Value = ([System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes(($Guid))))

 

NOTE: The PasswordCredential value is created as a Base64 value and is saved in the Azure Key Vault.

 

This process can also be completed via the Azure Portal but will take much more time to complete.