Licensing Office 365 via script

By | November 15, 2018
Powershell script that can be adapted to license Office 365 with various service plans.
Import-Module MSOnline  
Connect-MsolService -Credential $Credentials  
$SessionParams = @{ 
    ConfigurationName = "Microsoft.Exchange" 
    ConnectionUri = "https://outlook.office365.com/powershell-liveid/" 
    Credential = $Credentials 
    Authentication = "Basic" 
    AllowRedirection = $true} 
 
$Session = New-PSSession @SessionParams 
Import-PSSession -Session $Session -DisableNameChecking:$true -AllowClobber:$true | Out-Null 
#Get users into variable
$AllUsers = Get-MsolUser -DomainName domain.com -all 

$MyServicePlans = New-MsolLicenseOptions -AccountSkuId "tennant:STANDARDWOFFPACK_STUDENT" -DisabledPlans "EXCHANGE_S_STANDARD"

# license A1 without Exchange
$AllUsers | ForEach {
Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -RemoveLicenses "tennant:STANDARDWOFFPACK_STUDENT"
set-msoluser -UserPrincipalName $_.UserPrincipalName -UsageLocation GB
#Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses "tennant:STANDARDWOFFPACK_STUDENT"
Set-MsolUserLicense -UserPrincipalName $_.UserPrincipalName -AddLicenses "tennant:STANDARDWOFFPACK_STUDENT" -LicenseOptions $MyServicePlans
}