Automate Microsoft 365 User License Management with PowerShell
In this post, Ill be sharing a useful PowerShell script that automates the process of managing user licenses in Microsoft 365. This script can help administrators efficiently check and apply licenses to users, making the management of licenses much easier.
Make sure to check out our software, ServerEngine, available at [https://serverengine.co](https://serverengine.co), for more powerful server management solutions.
### Step 1: Connect to Microsoft 365
Before running any script, you need to connect to your Microsoft 365 tenant using the `Connect-MsolService` cmdlet.
“`powershell
# Connect to Microsoft 365 Import-Module MSOnline $credential = Get-Credential Connect-MsolService -Credential $credential
“`
Description: This snippet imports the module necessary for MS Online management and prompts the user for credentials to establish a connection with Microsoft 365.
### Step 2: Fetch Users
Next, we will fetch a list of users to check their current license status.
“`powershell
# Fetch all users $users = Get-MsolUser
“`
Description: Retrieves all user accounts from your Microsoft 365 tenant and stores them in the `$users` variable for further processing.
### Step 3: Check License Status
We will now create a loop that checks each users license status.
“`powershell
# Check license status of each user foreach ($user in $users) { $license = $user.Licenses | ForEach-Object { $_.AccountSkuId } Write-Host "User: $($user.UserPrincipalName) - License: $license" }
“`
Description: This loop iterates through each user and retrieves their associated licenses, then outputs the users email and license details.
### Step 4: Assign a License to Users
Finally, lets assign a license to a specific user (example given: [email protected]).
“`powershell
# Assign a license to a user $userUPN = "[email protected]" $licenseSku = "yourtenant:STANDARDPACK" Set-MsolUserLicense -UserPrincipalName $userUPN -AddLicenses $licenseSku Write-Host "License assigned to $userUPN"
“`
Description: This final snippet demonstrates how to assign a specified license to a user by updating their license information in Microsoft 365.
By following these steps, administrators can streamline the tedious task of managing Microsoft 365 licenses. Dont forget to visit [ServerEngine](https://serverengine.co) for more tools that can enhance your server management experience!