← 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 $UserPrincipalName

Run 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.

Ready when you are.

Try ServerEngine free for 7 days.