← script library
Efficient User Onboarding in Microsoft 365 with PowerShell
Microsoft 365 & Entra IDJanuary 4, 2025
Onboarding new users in Microsoft 365 can often be a time-consuming process, especially in larger organizations with many new hires. Automating this process can save significant administrative time and reduce the chances of errors. This PowerShell script will help you onboard new users by creating their accounts, assigning licenses, and adding them to specific groups in Microsoft 365.
powershell
# Install the Microsoft Graph module if it isnt already installed
Install-Module Microsoft.Graph -Force -AllowClobber
# Connect to Microsoft 365
Connect-MgGraph -Scopes User.ReadWrite.All, Group.ReadWrite.All, Directory.ReadWrite.All
# Define new user attributes
$UserPrincipalName = "[email protected]" # Replace with the users email address
$DisplayName = "New User"
$Password = "P@ssw0rd!" # Ensure this meets the password policy requirements
$LicenseSkuId = "yourtenant:ENTERPRISEPACK" # Replace with the appropriate SKU ID for licensing
$GroupId = "your-group-id" # Replace with the ID of the group to which the user will be added
# Create a new user in Microsoft 365
New-MgUser -AccountEnabled $true `
-DisplayName $DisplayName `
-MailNickName $DisplayName `
-UserPrincipalName $UserPrincipalName `
-GivenName "New" `
-Surname "User" `
-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.