Script to bulk remove a specific license from specific users

AB123 351 Reputation points
2023-09-08T12:39:40.6633333+00:00

Hi all,

I have using: ## Bulk Remove licenses ## ## Select Csv file $csv = Get-ChildItem -Path C:\temp\Office365Licence\Remove\ -File | Out-GridView -PassThru ## Import Csv $users = Import-Csv $csv.FullName ## Select Account SKU to be removed $accountSKU  = Get-MsolAccountSku | Select-Object AccountSkuId | Out-GridView -PassThru ## Loop through each user in the Csv foreach($user in $users){ Write-Host "Removing $($accountSKU.AccountSkuId) licence from $($user.UserPrincipalName)" -ForegroundColor Yellow ## Remove licence Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -RemoveLicenses $accountSKU.AccountSkuId }

Which removes license off bulk users however only does like 5 before it exceeds limit, i have read somewhere I need to usee the MSgraph to do this but what would the script be?

Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
5,292 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,681 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 44,506 Reputation points
    2023-09-11T14:52:45.01+00:00
    
    Hello there,
    
    Check with the below script
    
    # Install the Microsoft 365 PowerShell module if you haven't already
    # Install-Module -Name PowerShellGet -Force -AllowClobber
    # Install-Module -Name MSOnline -Force -AllowClobber
    
    # Connect to Microsoft 365 using PowerShell
    Connect-MsolService
    
    # Define the specific license you want to remove
    $licenseToRemove = "ENTER_LICENSE_SKU_NAME_HERE"
    
    # Define a list of user UPNs (User Principal Names) you want to remove the license from
    $usersToRemoveLicenseFrom = @(
        "*****************",
        "*****************",
        "*****************"
    )
    
    # Loop through each user and remove the specific license
    foreach ($user in $usersToRemoveLicenseFrom) {
        try {
            $userDetails = Get-MsolUser -UserPrincipalName $user
    
            # Check if the user has the specified license
            if ($userDetails.Licenses | Where-Object { $_.AccountSkuId -eq $licenseToRemove }) {
                Write-Host "Removing license $($licenseToRemove) from $($userDetails.UserPrincipalName)..."
                Set-MsolUserLicense -UserPrincipalName $user -RemoveLicenses $licenseToRemove
                Write-Host "License removed successfully."
            } else {
                Write-Host "User $($userDetails.UserPrincipalName) does not have the specified license."
            }
        } catch {
            Write-Host "Error removing license for $($user): $($_.Exception.Message)"
        }
    }
    
    # Disconnect from Microsoft 365
    Disconnect-MsolService
    
    
    Hope this resolves your Query !!
    
    --If the reply is helpful, please Upvote and Accept it as an answer–
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.