Create a new user in Azure AD with all licenses assigned

I’ve done some work with a customer recently where there was a requirement to provision new users in Azure Active Directory and assign all available licenses during the provisioning process (e.g. Office 365, Windows Intune).

It’s fairly straightforward to do with PowerShell.  The code below looks for the Azure subscription’s default domain (*.onmicrosoft.com) but you can obviously change that.  It’s also designed to provision one user at a time, prompting the administrator for the first and last name.  Remove the parameters and you can use the rest of the code to create users in bulk.

param (
   [Parameter(Mandatory=$False)]
   [ValidateNotNullOrEmpty()]
   [string]$userFirstName = $(Read-Host -Prompt 'Please Enter the First Name'),
 
   [Parameter(Mandatory=$False)]
   [ValidateNotNullOrEmpty()]
   [string]$userLastName = $(Read-Host -Prompt 'Please Enter the Last Name')
   )
 
$domainName = (Get-MsolDomain | Where-Object {$_.Name -like '*.onmicrosoft.com'}).Name
 
$userDisplayName = $userFirstName + ' ' + $userLastName
$userPrincipalName = $userFirstName + '.' + $userLastName + '@' + $domainName
$userPrincipalName = $userPrincipalName -replace ' ',''
 
$userTempPassword = 'P@ssw0rd'
$userUsageLocation = 'AU'
 
New-MsolUser -FirstName $userFirstName `
  -LastName $userLastName `
  -UserPrincipalName $userPrincipalName `
  -DisplayName $userDisplayName `
  -Password $userTempPassword `
  -UsageLocation $userUsageLocation | Out-Null
 
$licenses = Get-MsolAccountSku
 
foreach ($license in $licenses){
  Set-MsolUserLicense -UserPrincipalName $userPrincipalName -AddLicenses $license.AccountSkuId
}
 
Get-MsolUser -UserPrincipalName $userPrincipalName | Format-Table DisplayName,UserPrincipalName,Licenses -AutoSize

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>