PS: Script license assigning for Office 365

Wanted to share a script for assigning Licenses to Office 365 Users based on Security Groups in Your local Active Directory.


#Command One - Find users in local Security Group

$LyncGroup=Get-ADGroupMember -Identity 'LyncOnline' -Recursive | %{get-aduser $_.samaccountname} | Select UserPrincipalName

#$LyncGroup is just a placeholder for every users UserPrinciplaName found in the Security Group 'LyncOnline'
 #'LyncOnline' is the local Active Directory Security Group we collect members that should have Lync Online Plan assigned.

#Command Two - Find Users in Office 365.

$users = @(Get-MsolUser -All | ? { $_.userprincipalname -like "*"  })

#$users is the placeholder for all Office 365 users in my Azure Active Directory With the UPN-suffix

#Command Three - Find User and Assign License

foreach($user in $users){

if ($LyncGroup -contains $user.userprincipalname)
 write-host "Lync" $user.userprincipalname
 Set-MSolUser -UserPrincipalName $user.userprincipalname -UsageLocation NO
 Set-MSolUserLicense -userprincipalname $user.userprincipalname -Addlicenses "Apalnes365:MCOSTANDARD"

#For each user found in the placeholder $users Place them in placeholder $user and if that User also contains in placeholder $LyncGroup we will run the Set UsageLocation to NO (Norway) as it is mandatory for assigning a License in Office 365. And then we add the License, here represented With my tenant name and the attribute for Lync Online.


#Exchange Online

$ExchangeGroup=Get-ADGroupMember -Identity 'ExchangeUsers' -Recursive | %{get-aduser $_.samaccountname} | Select UserPrincipalName

$users = @(Get-MsolUser | ? { $_.userprincipalname -like "*" })
 foreach($user in $users){

if ($ExchangeGroup -contains $user.userprincipalname)
 write-host "Exchange" $user.userprincipalname
 Set-MSolUser -UserPrincipalName $user.userprincipalname -UsageLocation NO
 Set-MSoluserLicense -UserPrincipalName $user.userprincipalname -AddLicenses "Apalnes365:EXCHANGESTANDARD"



You will find all the Licenses With this PS Command:

Get-MsolAccountSku | Where-Object {$_.SkuPartNumber -eq “ENTERPRISEPACK”} |  ForEach-Object {$_.ServiceStatus}

Example Output:




I am Roy Apalnes, a Microsoft Cloud Evangelist working av Sopra Steria. Main focus in Microsoft Security and Endpoint Management, with a bigger picture in mind.

Featured Posts