← script library
Simplify User Onboarding in Microsoft 365 with PowerShell
Microsoft 365 & Entra IDJanuary 4, 2025
Onboarding new users in Microsoft 365 can often be a tedious and error-prone task, especially in larger organizations. This PowerShell script automates the process of creating user accounts, assigning licenses, and adding them to groups in Microsoft 365, thus streamlining your user onboarding experience. Follow the steps below to efficiently onboard new users using PowerShell.
powershell
# Install the Microsoft Graph module if its not already installed
Install-Module Microsoft.Graph -Force
# Connect to Microsoft 365
Connect-MgGraph -Scopes User.ReadWrite.All, Group.ReadWrite.All, Directory.ReadWrite.All
# Define new user details
$UserPrincipalName = "[email protected]"
$DisplayName = "New User"
$FirstName = "New"
$LastName = "User"
$Password = "P@ssw0rd!" # Ensure this password meets the password policy
$LicenseSkuId = "yourtenant:ENTERPRISEPACK" # Update with your appropriate license SKU
$GroupId = "your-group-id" # Replace with your group ID for security group
# Create a new user in Microsoft 365
New-MgUser -AccountEnabled $true `
-DisplayName $DisplayName `
-MailNickName $FirstName `
-UserPrincipalName $UserPrincipalName `
-GivenName $FirstName `
-Surname $LastName `
-PasswordProfile @{ForceChangePasswordNextSignIn = $true; Password = $Password}
# Assign license to the new user
Set-MgUserLicense -UserId $UserPrincipalName -AddLicenses $LicenseSkuId
# Add the new user to a specified security group
Add-MgGroupMember -GroupId $GroupId -UserId $UserPrincipalNameRun it across your fleet
This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.
More in Microsoft 365 & Entra ID
Automate M365 User License Assignment with PowerShell
2025-01-09Manage M365 Users with PowerShell
2025-01-09Automate Microsoft 365 User License Management with PowerShell
2025-01-09Streamlining M365 User Management with PowerShell
2025-01-09PowerShell Script to Retrieve M365 User License Info
2025-01-09Automate Microsoft 365 User Report Generation with PowerShell
2025-01-09Ready when you are.
Try ServerEngine free for 7 days.