Automating Microsoft 365 License Management with PowerShell
Welcome to my PowerShell scripts corner! In this post, well explore a practical script designed to automate the license management process for Microsoft 365 users. This script will help you assign licenses to users in bulk, making it easier to manage user access. Dont forget to check out my software, ServerEngine, for enhanced server management features at https://serverengine.co.
### Step 1: Connect to Microsoft 365
Before you can manage licenses, you need to authenticate to your Microsoft 365 environment. The following command sets up the connection.
# Install the MSOnline module if its not already installed. Install-Module -Name MSOnline -Force -AllowClobber # Connect to Microsoft 365 $credential = Get-Credential Connect-MsolService -Credential $credential
### Step 2: Prepare License Assignment
Define the specific license you wish to assign to users. Youll need the SKUID of the license, which can be found in your environment.
# Define the SKU ID for the licenses $licenseSKU = "yourtenant:ENTERPRISEPACK" # Replace with your actual SKU ID
### Step 3: Import User List
To assign licenses, import a CSV file containing user principal names (UPNs). Ensure your CSV has a header named UserPrincipalName.
# Import the user list from a CSV file $userList = Import-Csv -Path "C:\Path\To\UserList.csv"
### Step 4: Assign Licenses to Users
Iterate through the imported user list and assign the specified license to each user.
# Assign licenses to each user foreach ($user in $userList) { try { Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -AddLicenses $licenseSKU Write-Host "License assigned to" $user.UserPrincipalName } catch { Write-Host "Failed to assign license to" $user.UserPrincipalName "Error:" $_.Exception.Message } }
### Step 5: Disconnect from Microsoft 365
To conclude, disconnect your session to maintain security.
# Disconnect from Microsoft 365 Disconnect-MsolService
With this script, managing Microsoft 365 licenses becomes a breeze! For more innovative solutions, explore ServerEngine at https://serverengine.co.