Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 7025

Deprecated: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 2162

Deprecated: Hook imagify_allow_picture_tags_for_webp is deprecated since version 2.2! Use imagify_allow_picture_tags_for_nextgen instead. in /www/collab365_296/public/wp-includes/functions.php on line 5758
How to add (or remove) Office 365 licenses in bulk with Powershell - Collab365
Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Deprecated: strstr(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1145

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1152

Deprecated: stripos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1155

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1162

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /www/collab365_296/public/wp-includes/functions.php on line 1165

Warning: Undefined array key "url" in /www/collab365_296/public/wp-content/plugins/thrive-visual-editor/thrive-dashboard/inc/smart-site/classes/class-tvd-smart-shortcodes.php on line 85

2017-01-12

Had a look around internet and couldn’t find anywhere a script to add/remove Office 365 licenses in bulk using PowerShell.  There were a lot of examples about how to set these fresh (without consideration for what is already set), so quickly conjured this up…. Someone may find it helpful.

A quick note on the scenario – The company I am working at had already disabled some licenses for some users.  I was asked to disable PowerApp and Flow.  Now I couldn’t simply set the license options as follows.

$accountID = (Get-solAccountSku).AccountSkuId 
# Disabled Plans
$disabledPlans= @()
$disabledPlans +="FLOW_O365_P2" 
$licenseOptions = New-MsolLicenseOptions -AccountSkuId $accountID -DisabledPlans $disabledPlans;
$licensedUsers = Get-MsolUser -All | ? {$_.isLicensed -eq "TRUE"}
$licensedUsers | Set-MsolUserLicense -LicenseOptions $licenseOptions

This would enable anything else that was previously disabled.

In summary, I needed to loop through existing disabled licences and add to $disabledPlans before I could set the licenses.  Here is the code to disable PowerApps and Flow (apologies about the formatting).

# $cred = Get-Credential 
# Connect-MsolService -Credential $cred 
$licensedUsers = Get-MsolUser -All | ? {$_.isLicensed -eq "TRUE"} 
ForEach ($licensedUser in $licensedUsers) 
{
Write-Host "---------------------------------" 
Write-Host "UserPrincipalName-"$licensedUser.UserPrincipalName 
$accountSku = $licensedUser.Licenses.AccountSkuId 
Write-Host $accountSku 
# Disabled Plans 
$disabledPlans= @() 
$disabledPlans +="FLOW_O365_P2" 
$disabledPlans +="POWERAPPS_O365_P2" 
$licensesAssigned = $licensedUser.Licenses.ServiceStatus 
foreach ($serviceStatus in $licensesAssigned) 
{ 
# A bit of logic to check if its not already disabled as we don't want to add twice 
if($serviceStatus.ProvisioningStatus -eq "Disabled")
{
$planName = $serviceStatus.ServicePlan.ServiceName 
Write-Host "Already Disabled - "$planName 
if($planName -eq "FLOW_O365_P2" -or $planName -eq "POWERAPPS_O365_P2") 
{
# Do nothing as already added above
}
else
{
# Add to array as we want to keep it disabled 
$disabledPlans += $planName
}
}
}
# Now ready to create license Options and disable plabs for that user 
Write-Host "Disabling following licenses for user" 
Write-Host $disabledPlans
$licenseOptions = New-MsolLicenseOptions -AccountSkuId $accountSku -DisabledPlans $disabledPlans; 
Set-MsolUserLicense -UserPrincipalName $licensedUser.UserPrincipalName -LicenseOptions $licenseOptions
}

 

(Visited 1,025 times, 1 visits today)

About the author 

Qamar Zaman

Summit Bundle

Get 200+ hours of Microsoft 365 Training for 27$!

Master Office 365, Power Platform & SharePoint & Teams With 200+ Hours Of Training Videos and 108 Ebooks in the Collab365 Academy. This offer is insane and is only available for a limited period.