Automate M365 User License Assignment with PowerShell
Welcome to our PowerShell scripts section! In this post, were sharing a handy PowerShell script that automates the assignment of licenses to Microsoft 365 users in bulk. Whether youre managing a small team or overseeing an entire organization, this script can save you time and effort while ensuring that users have the appropriate licenses for their roles.
Step 1: Connect to Microsoft 365
To begin, you need to authenticate and connect to your Microsoft 365 tenant. This step ensures that you have the necessary permissions to manage user licenses.
“`powershell
# Install the AzureAD module if not already installed Install-Module -Name AzureAD -Force -AllowClobber # Connect to Azure AD Connect-AzureAD
“`
Step 2: Retrieve Users Without Licenses
Next, we collect a list of users who currently do not have any licenses assigned. This will help us target what users we need to modify.
“`powershell
# Get all users without any licenses $usersWithoutLicenses = Get-AzureADUser -All $true | Where-Object { -not $_.AssignedLicenses } # Display the count of users without licenses $usersWithoutLicenses.Count
“`
Step 3: Define the License SKU
Now, we define the SKU ID of the Microsoft 365 license we want to assign. You can find the SKU ID for different licenses through the Azure portal or by running a command.
“`powershell
# Define the License SKU ID (e.g., "ENTERPRISEPACK" for E3) $skuId = "ENTERPRISEPACK"
“`
Step 4: Assign Licenses to Users
Now its time to assign the specified license to all users who are currently unlicensed. This loop processes each user and updates their licenses.
“`powershell
# Assign the license to each user foreach ($user in $usersWithoutLicenses) { Set-AzureADUserLicense -ObjectId $user.ObjectId -AddLicenses $skuId Write-Host "Assigned license to user: $($user.UserPrincipalName)" }
“`
Step 5: Confirm License Assignment
Finally, lets verify that the licenses have been successfully assigned by checking the users license status.
“`powershell
# Check and display users with assigned licenses $updatedUsers = Get-AzureADUser -All $true | Where-Object { $_.AssignedLicenses } $updatedUsers | ForEach-Object { Write-Host "$($_.UserPrincipalName) has licenses assigned." }
“`
By utilizing this PowerShell script, you can efficiently manage Microsoft 365 licenses, enhancing productivity and ensuring compliance within your organization.
To further streamline your server management and enhance your IT operations, check out ServerEngine, a powerful tool designed to simplify server tasks. Visit our website at [https://serverengine.co](https://serverengine.co) to learn more, access documentation, and get support. Happy scripting!