Change Office 365 Products for a Group of Users with PowerShell

I wanted to switch all my users over to Office 365 Business Premium. It’s a bundle deal for companies of up to 300 people. Previously I’d been separately licencing each of Office 365 ProPlus, Skype for Business and OneDrive, but the Business Premium bundle is quite a bit cheaper, and gives you lots more apps.

I wanted to migrate the users over in batches, so wrote this script to help me. I created a new Office 365 security group to ToBusPrem (you can see this name in the script), added the users I wanted to migrate into that, then ran the script.

The script needs the creds of an Office 365 Global Admin, and prompts for them when it’s run. It also requires you to have the Microsoft Online Services Sign-in Assistant for IT Professionals  and Microsoft Azure Active Directory Module for Windows PowerShell installed.

It then gets all the users in the security group and transfers them over one by one.
Some of my users have extra licenses, e.g. Visio, Project, Audio Conferencing. The script works by removing all licenses that a user has, so I exclude Visio and Project from that list.

That won’t work for Audio Conferencing as the licence is only valid if an Office Suite licence is currently allocated. So I set a flag if Audio Conferencing is present, and use the flag to add audio conferencing back after the Business Premium licence has been added.

$MsolCred = Get-Credential
Connect-MsolService -Credential $MsolCred

$MigrationGroupId = (Get-MsolGroup -GroupType Security | Where-Object -Property DisplayName -EQ "ToBusPrem").ObjectId
$MigrationUsers = (Get-MsolGroupMember -GroupObjectId $MigrationGroupId).EmailAddress
$MigrationUsers.Count

$MsolDomain = "RCMTECHCOUK"
$MsolAccountSkuIdsToAdd = $MsolDomain+":O365_BUSINESS_PREMIUM"

foreach($MsolUPN in $MigrationUsers){
    Write-Host $MsolUPN -NoNewline
    $AccountLicenses = Get-MsolUser -UserPrincipalName $MsolUPN | foreach {$_.Licenses}
    $AccountLicenses = $AccountLicenses | Where-Object -Property AccountSkuId -notlike "*VISIOCLIENT" # Exclude Visio
    $AccountLicenses = $AccountLicenses | Where-Object -Property AccountSkuId -notlike "*PROJECTCLIENT" # Exclude Project
    if($AccountLicenses.AccountSkuId -like "*MCOMEETADV"){$AudioConferencing = $true}else{$AudioConferencing = $false} # Flag if Audio Conferencing user
    Write-Host " Remove" -ForegroundColor Gray -NoNewline
    Set-MsolUserLicense -UserPrincipalName $MsolUPN -RemoveLicenses $AccountLicenses.AccountSkuId # Remove all products
    Write-Host " Add" -ForegroundColor Gray -NoNewline
    $MsolLicenseOptions = New-MsolLicenseOptions -AccountSkuId $MsolAccountSkuIdsToAdd -DisabledPlans "EXCHANGE_S_STANDARD","MICROSOFTBOOKINGS","O365_SB_Relationship_Management"
    Set-MsolUserLicense -UserPrincipalName $MsolUPN -AddLicenses $MsolAccountSkuIdsToAdd -LicenseOptions $MsolLicenseOptions
    if($AudioConferencing){Set-MsolUserLicense -UserPrincipalName $MsolUPN -AddLicenses ($MsolDomain+":MCOMEETADV")} # Add Audio Conferencing if flagged
    Write-Host " Done" -ForegroundColor Gray
}
Advertisements
This entry was posted in PowerShell and tagged , , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s