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