Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save darrenjrobinson/3a86358c418701fa88e38cfbd149bc4f to your computer and use it in GitHub Desktop.
Save darrenjrobinson/3a86358c418701fa88e38cfbd149bc4f to your computer and use it in GitHub Desktop.
# Install the AzureAD Preview Module
# Install-Module -Name AzureADPreview -MinimumVersion 2.0.0.7 -Force -SkipPublisherCheck
# Import the Module
Import-Module AzureADPreview -RequiredVersion 2.0.0.7
# Tenant Name
$tenantID = "tenant.customer.com"
# Admin User for the Tenant
$username = "admin@tenant.customer.com"
# Password for the Admin User
$password = 'MyP@ssw0rd!' | convertto-securestring -AsPlainText -Force
# Credentials for the Admin User
$credentials = New-Object System.Management.Automation.PSCredential $Username,$password
#Connect to your AzureAD tenant
Connect-AzureAD -TenantId $tenantID -Credential $credentials
# Get the License SkuId from a similar user that we want to apply to the new user
$licensedUser = Get-AzureADUser -ObjectId "templateuser@tenant.customer.com"
$licensedUser.AssignedLicenses
# Get the New User we want to apply the license too
$user = Get-AzureADUser -ObjectId "newuser@tenant.customer.com"
# check to see they have no license
$user.AssignedLicenses
# Create the new License Object
$license = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicense
$license.SkuId = $licensedUser.AssignedLicenses.SkuId
# Create the Licenses Table and add the license from above
$licenses = New-Object -TypeName Microsoft.Open.AzureAD.Model.AssignedLicenses
$licenses.AddLicenses = $license
# Apply the license to the new user
Set-AzureADUserLicense -ObjectId $user.ObjectId -AssignedLicenses $licenses
# check to see license has applied
$user = Get-AzureADUser -ObjectId $user.ObjectId
$user.AssignedLicenses
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment