Disable Office365 services

I have found a code and edited it to disable sway, planner, yammer and skype for ofiice365 licenses. I want to be able to apply this to all users for that particular license. Currently the code only does it for one user. I also want to be able to make it into a function to get ad users in a OU and then run the script for each user in that OU

$upn = “test@testmail.com”

Get the current licenses (this pulls the top level, like Office 365 Enterprise, Project Online etc.

$licensedetails = (Get-MsolUser -UserPrincipalName $upn ).Licenses

Use an array to pull all of the disabled plans together – and start with the one you want to disable

$ldo = @()
$ldo += “SWAY”, “YAMMER_EDU”, “PROJECTWORKMANAGEMENT”, “MCOSTANDARD”
$licensedetails.Count;

Making sure there are some licenses

if ($licensedetails.Count -gt 0){
foreach ($ld in $licensedetails){

Edit the next line with your tenant

if ($ld.AccountSkuId -eq “tenant:STANDARDWOFFPACK_IW_STUDENT”){
foreach ($lds in $ld.ServiceStatus){
if ($lds.ProvisioningStatus -eq “Disabled”){

I’m looking for ones that are already disabled and adding them to my array

$ldo = $ldo + $lds.ServicePlan.ServiceName.ToString()
}
}
}
}
}

I then create a new license option – and set the DisabledPlans to my array – Planner plus the others it finds

$LO = New-MsolLicenseOptions -AccountSkuId “tenant:STANDARDWOFFPACK_IW_STUDENT” -DisabledPlans $ldo

Finally I set the license option for my selected user – disabling Sway, Yammer, Planner and Skype.

Set-MsolUserLicense -UserPrincipalName $upn -LicenseOptions $LO

This might get you started:

$TenantName = "Tenant"
$Sku = "$TenantName`:STANDARDWOFFPACK_IW_STUDENT"
$ToDisable = "SWAY", "YAMMER_EDU", "PROJECTWORKMANAGEMENT", "MCOSTANDARD"

$Users = Get-MsolUser -All

foreach ($User in $Users)
{
    if ($User.Licenses.AccountSkuId -contains $Sku)
    {
        $Disabled = (($User.Licenses | Where-Object {$_.ServiceStatus.ProvisioningStatus -eq 'Disabled'}).ServicePlan).ServiceName
        $Disabled = $Disabled + $ToDisable | Select-Object -Unique
        $Options = New-MsolLicenseOptions -AccountSkuId $Sku -DisabledPlans $Disabled
        Set-MsolUserLicense -UserPrincipalName $User.UserPrincipalName -LicenseOptions $Options
    }
}

This will run against all your Office 365 users and only take action on those that have the specified Sku applied. Make sure you replace the value of $TenantName with your own tenant. Also, you may want to make sure this does exactly what you want by filtering down to one user or a few users first. The MSOL cmdlets don’t support -WhatIf, so you’re on your own to verify that your actions are correct.

I’ve done things like this in a function before, if I get a moment today I may see if I can expand on the above in a way that makes sense. Also, I’ve come to expect some errors whenever I do bulk licensing in Office 365, so you may need to add in some code to handle those without stopping the whole process. For more details, see my blog series on Office 365 licensing with PowerShell.

Thanks Matt. I will have a go

Hi Matt,

I have tried the code and yep i got errors when doing a bulk license. The error is:

Set-MsolUserLicense : Cannot bind argument to parameter ‘UserPrincipalName’
because it is null.
At line:16 char:48

  •     Set-MsolUserLicense -UserPrincipalName $upn -LicenseOptions $ ...
    
  •                                            ~~~~
    
    • CategoryInfo : InvalidData: (:slight_smile: [Set-MsolUserLicense], Paramete
      rBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,M
      icrosoft.Online.Administration.Automation.SetUserLicense

I am very new to powershell so any help would be much appreciated.

How are you populating $UPN? The error states that this value is null.

for the original code - I had the $UPN defining the email address of one user. I no longer want this as I want to update this to all users

Ahh… I see now. I copied and pasted some of your original code and forgot to update the value for the -UserPrincipalName parameter. I fixed this above - see if it works for you now.

the code runs and I don’t get any errors but I’ve encountered a weird issue. For some users it has disabled the services, but for some it only disables 1/2 services and for some it hasn’t disabled it at all

I have updated the &TenantName variable to the one for my organisation. From running the Get-MsolAccountSku I have checked to make sure “STANDARDWOFFPACK_IW_STUDENT” is the licence applied to user accounts.

i have edited the code so that it only disables Planner = Projectworkmanagement. when I run the code it has disabled this as I wanted it to. but if I add Sway and Yammer it doesn’t disable those services.

Hi

Try following, I removed IF sentence, since we get only users with that license at the beginning into $users

#Untested

$TenantName = "Tenant"
$Sku = "$TenantName`:STANDARDWOFFPACK_IW_STUDENT"
$ToDisable = "SWAY, YAMMER_EDU, PROJECTWORKMANAGEMENT, MCOSTANDARD"

$Users = Get-MsolUser -All  | Where-Object {$_.Licenses.AccountSkuID -eq $Sku}

foreach ($User in $Users)
{
        $Disabled = New-Object System.Collections.Generic.List[String]
        $Disabled.Add((($user).Licenses.ServiceStatus | where {$_.ProvisioningStatus -eq 'Disabled'}).ServicePlan.ServiceName)
        $Disabled.Add($($ToDisable)
        $Disabled = $Disabled | Select-Object -Unique

        $Options = New-MsolLicenseOptions -AccountSkuId $Sku -DisabledPlans $Disabled

            Set-MsolUserLicense -UserPrincipalName $User.UserPrincipalName -LicenseOptions $Options
}

Jake

Thank you. before I try the above code i want to have a code that enables the services but don’t know where to start on editing the above code. Any help will be much appreciated.

Hi

What you mean enables the services? I think it is enough to add license without options so it will add full license.

Jake

I mean I have a code that disables the planner licence. if in the future I want this feature enabled again for all users - i would like a code that would do this for all users. e.g. SWAY is currently disabled for all users. we are thinking of enabling this for all students. This is a code that I have, but it only does it for one user:

$upn = “user@domain.com”
$licensedetails = (Get-MsolUser -UserPrincipalName $upn ).Licenses
$ldo = @()
$licensedetails.Count;
if ($licensedetails.Count -gt 0){
foreach ($ld in $licensedetails){
if ($ld.AccountSkuId -eq “tenant:STANDARDWOFFPACK_IW_STUDENT”){
foreach ($lds in $ld.ServiceStatus){
if ($lds.ProvisioningStatus -eq “Disabled”){
if ($lds.ServicePlan.ServiceName -ne “SWAY”){
$ldo = $ldo + $lds.ServicePlan.ServiceName.ToString()
}
}
}
}
}
}
$LO = New-MsolLicenseOptions -AccountSkuId “tenant:STANDARDWOFFPACK_IW_STUDENT” -DisabledPlans $ldo
Set-MsolUserLicense -UserPrincipalName $upn -LicenseOptions $LO

Hi

Just use the script that removes the service, but on $Disabled remove the service name from variable.

Jake

Hi Jake,

I’m not sure what you mean. The above code only enables the service for one user but I want to be able to enable it for all users.

Thanks

Hi

Use the one that I posted, that will go through all users you wanted. Then remove wanted servive from $Disabled.

Jake

Hi Jake,

I’m still not sure which part of $disabled to remove.

Thanks

Hi

I ment removing the wanted one from $Disabled so it will be enabled again. I think that the String is not the ideal for this purpose, because we cannot easily identify where that value is, when the String is something1, something2, something4, something3. So we cannot determ is the value we need to remove st the beginning, middle of at the end, so we cnnot easily remove the comma. So I think that if we create instead a hashtable where we could add values and easily remove the wanted one and after that convert the hashtable to string.

Edit. I removed the old post because it was not working.

Jake