Skip to content

Instantly share code, notes, and snippets.

@rheid
Created January 31, 2017 15:14
Show Gist options
  • Save rheid/59674c4cd14fc3446ff1247dd5d9f532 to your computer and use it in GitHub Desktop.
Save rheid/59674c4cd14fc3446ff1247dd5d9f532 to your computer and use it in GitHub Desktop.
Disable Office 365 Service
#MAKE SURE YOU ARE CONNECTED TO OFFICE 365 BEFORE RUNNING THIS SCRIPT
#If you don't know how check out: http://powershell.office.com/script-samples/connect-to-Azure-AD
#The initial script was built for Yammer removal by Vlad Catrinescu and can be found here: http://spvlad.com/1VXll7f
#Updated by Drew Madelung to support O365 Planner GA
#This script will go through all licensed users and first check if they have the planner preview license and remove it and then check for the GA O365 planner license and disable it
#This script will re-enable all other services EXCEPT planner, If you want to disable more add them comma seaparated to the $disabledplans variable.
#For example to disable Yammer and Planner use this: $disableplans = "PROJECTWORKMANAGEMENT", "YAMMER_ENTERPRISE"
#Set disabled plans (only Planner to start)
$disableplans = "PROJECTWORKMANAGEMENT"
#Get All Licensed Users
$users = Get-MsolUser | Where-Object {$_.isLicensed -eq $true}
foreach ($user in $users)
{
Write-Host "Checking " $user.UserPrincipalName -foregroundcolor "Cyan"
$CurrentSku = $user.Licenses.Accountskuid
#If more than one SKU, Have to check them all!
if ($currentSku.count -gt 1)
{
Write-Host $user.UserPrincipalName "Has Multiple SKU Assigned. Checking all of them" -foregroundcolor "White"
for($i = 0; $i -lt $currentSku.count; $i++)
{
#Disable preview planner if it is assigned to this user
if($currentSku[$i] -like "*PLANNERSTANDALONE*" )
{
$pos = $currentSku[$i].IndexOf(":")
$tenant = $currentSku[$i].Substring(0, $pos)
$license = $tenant + ":PLANNERSTANDALONE"
Write-host $user.Licenses[$i].AccountSkuid "has Planner Preview. Will Disable for" $tenant -foregroundcolor "Yellow"
Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -RemoveLicenses $license
Write-Host "Planner Preview disabled for " $user.UserPrincipalName " On SKU " $user.Licenses[$i].AccountSkuid -foregroundcolor "Green"
}
else
{
#Loop trough Each SKU to see if one of their services has the word PROJECTWORKMANAGEMENT inside. This is the service for O365 Planner
if($user.Licenses[$i].ServiceStatus.ServicePlan.ServiceName -like "*PROJECTWORKMANAGEMENT*" )
{
Write-host $user.Licenses[$i].AccountSkuid "has Planner. Will Disable" -foregroundcolor "Yellow"
$NewSkU = New-MsolLicenseOptions -AccountSkuId $user.Licenses[$i].AccountSkuid -DisabledPlans $disableplans
Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -LicenseOptions $NewSkU
Write-Host "Planner disabled for " $user.UserPrincipalName " On SKU " $user.Licenses[$i].AccountSkuid -foregroundcolor "Green"
}
else
{
Write-host $user.Licenses[$i].AccountSkuid " doesn't have Planner. Skip" -foregroundcolor "Magenta"
}
}
}
}
else
{
$NewSkU = New-MsolLicenseOptions -AccountSkuId $CurrentSku -DisabledPlans PROJECTWORKMANAGEMENT
Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -LicenseOptions $NewSkU
Write-Host "Planner disabled for " $user.UserPrincipalName -foregroundcolor "Green"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment