Replace Office 365 licenses with Powershell RRS feed

  • Question

  • Hi,

    We are still in the process of running pilot tests and gradually rolling out Service Plans.  We are using ADFS.  I put this short script together to assign limited licenses to members of a security group.  The script does not interfere with existing licenses.

    # Set-E1.S4b.Sharepoint.Only.ps1

    # Assigns E1 (STANDARDPACK) licenses to specified group but only enables Skype for Business (S4B) and Sharepoint.
    # The new license and options are only applied to unlicensed users in the specified group.  Licensed users are ignored.
    # This is helpful when the goal is to provide unlicensed users with the standard license, but not change the
    # licenses of users who are already licensed.

    # First set the variable for the plans to be disabled
    # This example is for our E1 licenses as the included ServicePlans will vary

    # For a single, specific user, run the following
    # Set-MsolUserLicense -UserPrincipalName user@healthpartners.org -LicenseOptions $options  
    # The following should work with a security or distribution group
    # Use Get-MsolGroup | Where-Object {$_.DisplayName -eq <"DisplayName">
    # to get the GroupObjectID
    # Note that the script would throw an error when a group member is already licensed.  The Where-Object test tells it to skip
    # licensed users, thereby suppressing error messages.

    $members = Get-MsolGroupMember -GroupObjectId d6d89e16-2c72-4df6-8d02-11c2c38a116f | Where-Object { $_.IsLicensed -eq $false }
    foreach ($msolUser in $members) {

        # Set usage location. This is required for every individual user.

        Set-MsolUser -UserPrincipalName $msolUser.UserPrincipalName -ObjectId $msolUser.ObjectId -UsageLocation "US";
        # Apply S4B-only license to all group members

        Set-MsolUserLicense -objectID $msolUser.ObjectId -AddLicenses hpplans:STANDARDPACK -LicenseOptions $options;


    I want to expand the script to be used to remove licenses from already licensed users and then assign the limited E1 licenses as above.  I won't know what licenses are already assigned, so I want to store the existing license as a variable so that I can pass that variable to Get-MsolUser -RemoveLicenses.  I've been trying to first get it to work for a single user before expanding it to a group, and it currently fails without error or warning.  Any idea what I'm doing wrong?

    $licenseDetails = (Get-MsolUser -UserPrincipalName "fperson-test@hpplans.onmicrosoft.com").Licenses

    # $licenseString = Out-String -InputObject $licenseDetails.AccountSkuId;

    Set-MsolUserLicense -UserPrincipalName "fperson-test@hpplans.onmicrosoft.com" -RemoveLicenses $license.AccountSkuId
    Monday, October 31, 2016 8:48 PM

All replies

  • You haven't just missed $licenseDetails? When you are using the RemoveLicenses parameter, you are using $license.AccountSkuID as your variable. Shouldn't it be $licenseDetails.AccountSkuId?
    Saturday, November 5, 2016 3:05 AM