Auto Assign Office 365 License based on domain name


Problem: Customer has a few email domain names and are slowly migrating to Office 365. The customer wants to auto assign license for certain domains using PowerShell.
Step 1:
Set the Office 365 tenant password in a TXT file.
The PowerShell Script:
#Modify below YOURPASSWORD to your Office 365 password
$password = “YOURPASSWORD”
$password | ConvertFrom-SecureString | Set-Content c:\o365\password.txt
Step 2:
Search based on the valid domains and add license for users that have not been licensed.
The powershell script:
#Valid Domains.
#Modify below domainA.com and domainB.com to your own domain that you want to auto assign license.
$validDomains =”*@domainA.com”,”*@domainB.com”
$MsolAdmUser = “admin@USERTENANTNAME.onmicrosoft.com”
$pwd = Get-Content c:\o365\credmsol.txt | ConvertTo-SecureString
$cred = New-Object System.Management.Automation.PSCredential $MsolAdmUser, $pwd
# CONNECT TO 365
Import-Module MSOnline
Connect-MsolService -Credential $cred
$temp = (get-msoluser -all) | select userprincipalname
foreach ($a in $validDomains){
foreach ($b in $temp){
$validUser = ($b) | where {$b.userprincipalname -like $a}
If ($validUser –eq $null){
} else{
set-msoluser -userprincipalname $validUser.userprincipalname -usagelocation “MY”
set-msoluserlicense -userprincipalname $validUser.userprincipalname -addlicenses “userTenantName:STANDARDWOFFPACK“
$validUser = $null
}
}
}
To get the “userTenantName:STANDARDWOFFPACK“, you will need to run get-msolaccountsku.
The above is a script that I quickly developed. Apologies if it isn’t neat as of now 🙂
Posted in Office365 | Comments Off on Auto Assign Office 365 License based on domain name