Managing licenses in Microsoft 365 can be a time-consuming task, especially when handling a large number of users with varying roles and departments. Automating this process with Graph PowerShell can significantly streamline your administration tasks. Below is a straightforward script to automate the assignment and removal of licenses based on user roles or department changes.
Install-Module -Name Microsoft.Graph -Force -AllowClobber
.Connect-MgGraph -Scopes "User.ReadWrite.All" "Directory.ReadWrite.All"
# Install the Microsoft.Graph module if not already installed
Install-Module -Name Microsoft.Graph -Force -AllowClobber
# Connect to Microsoft 365
Connect-MgGraph -Scopes "User.ReadWrite.All" "Directory.ReadWrite.All"
# Define the SKU ID for the license to be assigned/removed
$skuId = "your-sku-id-here" # Replace with your specific SKU ID
# Define filters based on roles or departments
$assignFilter = "Department eq 'Sales'"
$removeFilter = "Department eq 'HR'"
# Get users for assigning licenses
$salesUsers = Get-MgUser -Filter $assignFilter -ConsistencyLevel eventual -All
# Assign licenses to Sales department users
foreach ($user in $salesUsers) {
$licenseDetails = @{
AddLicenses = @(@{SkuId = $skuId})
RemoveLicenses = @()
}
Set-MgUserLicense -UserId $user.Id -AddLicenses $licenseDetails.AddLicenses -RemoveLicenses $licenseDetails.RemoveLicenses
Write-Output "License assigned to user: $($user.UserPrincipalName)"
}
# Get users for removing licenses
$hrUsers = Get-MgUser -Filter $removeFilter -ConsistencyLevel eventual -All
# Remove licenses from HR department users
foreach ($user in $hrUsers) {
$licenseDetails = @{
AddLicenses = @()
RemoveLicenses = @($skuId)
}
Set-MgUserLicense -UserId $user.Id -AddLicenses $licenseDetails.AddLicenses -RemoveLicenses $licenseDetails.RemoveLicenses
Write-Output "License removed from user: $($user.UserPrincipalName)"
}
# Disconnect from Microsoft 365
Disconnect-MgGraph
Prerequisites:
Connect-MgGraph
command initiates a connection to Microsoft 365 with the required scopes for managing user licenses.Define SKU ID:
$skuId
variable holds the SKU ID of the license you want to assign or remove. Replace "your-sku-id-here" with the actual SKU ID for the desired license. Run Get-MgSubscribedSku -All command to get the SKU ID.Define Filters:
$assignFilter:
Filter to select users from the Sales department.$removeFilter:
Filter to select users from the HR department.Get Users and Assign Licenses:
Set-MgUserLicense
.Get Users and Remove Licenses:
Set-MgUserLicense
.Disconnect:
Disconnect-MgGraph
command ends the session with Microsoft 365.Automating license management in Microsoft 365 using Graph PowerShell simplifies administrative tasks, saving time and reducing the potential for human error. This script provides a straightforward way to assign and remove licenses based on user roles or department changes. With further enhancements, this script can become an even more powerful tool in your administrative toolkit.
Embrace automation and streamline your Microsoft 365 management with the power of Graph PowerShell!
© m365corner.com. All Rights Reserved. Design by HTML Codex