Modify-Sublicense
An example call would be:
Modify-Sublicense -upn "joe@yourcompany.com" -PrimaryLicense "TENANT:ENTERPRISEPACK" -SublicensesToRemove @("SWAY","YAMMER_ENTERPRISE") -SublicensesToAdd @("SHAREPOINTENTERPRISE", "SHAREPOINTWAC")
If you’re new to powershell, scroll down past the code below for some additional tips.
Naturally, sublicenses you had before will REMAIN unless you’ve removed them.
AND
Sublicenses you DID NOT HAVE before will NOT BE ADDED unless you specifically add them.
This is important because of how the licensing works, which has been covered in other blog posts my myself and others.
I’ve used the function below as part of a larger script with good results.
As always, TEST BEFORE YOU USE IT!
##---------------------------------------------------------------------
## this function Adds and or removes sublicenses
## Pass in the -SublicensesToAdd and -SublicensesToRemove as an array ( use $var = @("value", "Value") )
## to a user's Pre-Existing License (for example TENANT:ENTERPRISEPACK)
##---------------------------------------------------------------------
function Modify-SubLicense($upn, $PrimaryLicense, $SublicensesToAdd, $SublicensesToRemove)
{
$spouser = get-msoluser -userprincipalname $upn
#assemble a list of sub-licenses types the user has that are currently disabled, minus the one we're trying to add
$disabledServices = $($spouser.Licenses | where {$_.AccountSkuID -eq $PrimaryLicense}).servicestatus | where {$_.ProvisioningStatus -eq "Disabled"} | select -expand serviceplan | Select ServiceName
#disabled items need to be in an array form, next 2 lines build that...
$disabled = @()
foreach ($item in $disabledServices.servicename) {$disabled += $item}
write-host " DisabledList before changes: $disabled" -foregroundcolor yellow
#if there are other sublicenses to be removed (Passed in via -SublicensesToRemove) then lets add those to the disabled list.
foreach ($license in $SublicensesToRemove)
{
$disabled += $license
}
#cleanup duplicates in case the license to remove was already missing
$disabled = $disabled | select -unique
#If there are licenses to ADD, we need to REMOVE them from the list of disabled licenses
# http://stackoverflow.com/questions/8609204/union-and-intersection-in-powershell
$disabled = $disabled | ?{$SublicensesToAdd -notContains $_}
write-host " DisabledList after changes: $Disabled" -foregroundColor green
$LicenseOptions = New-MsolLicenseOptions -AccountSkuId $PrimaryLicense -DisabledPlans $disabled
set-msoluserlicense -userprincipalname $upn -licenseoptions $LicenseOptions
}
New to powershell?
Here are a few tips:
- Since the code is a function, you’ll need to copy-paste it to a script of your own before you can use it
- In powershell, functions need to appear in your script above/before you use them.
- There are ways to load the function in memory, so you can call it as if it was a native command. See How to add functions to your powershell session
- An array in powershell can have zero or 1 or many items, if you need to pass a single value, just pass it as an array with one value. That would look like this:
@("Value1", "Value2")
– Jack