Automating User Onboarding with Microsoft Entra ID and PowerShell
User onboarding is a critical process that can often be time-consuming and prone to error. By automating the onboarding process, organizations can ensure new hires are set up quickly and efficiently. This PowerShell script is designed to create new user accounts in Microsoft Entra ID, assign roles, and ensure necessary settings are configured. This script will: 1. Create a new user in Microsoft Entra ID with specified credentials. 2. Assign the user to default groups for access. 3. Send a welcome email with account details and instructions. By implementing this script, IT administrators can streamline the onboarding process, allowing new users to become productive quicker.
# Import the Microsoft Graph module
Install-Module Microsoft.Graph -Scope CurrentUser -AllowClobber -Force
Import-Module Microsoft.Graph
# Connect to Microsoft Graph
Connect-MgGraph -Scopes 'User.ReadWrite.All', 'Group.ReadWrite.All'
# Parameters for the new user
$newUserPrincipalName = "[email protected]"
$newUserDisplayName = "New User"
$newUserPassword = "P@ssword123!" # Generate a secure password
$groupId = "your-default-group-id"
# Create the new user
$newUser = New-MgUser -UserPrincipalName $newUserPrincipalName -DisplayName $newUserDisplayName -AccountEnabled $true -MailNickname "newuser" -PasswordProfile @{
ForceChangePasswordNextSignIn = $true
Password = $newUserPassword
}
# Assign the user to the default group
Add-MgGroupMember -GroupId $groupId -DirectoryObjectId $newUser.Id
# Optional: Send a welcome email
$welcomeMessage = "Welcome $newUserDisplayName! Your account has been created. Log in using: Username: $newUserPrincipalName Password: $newUserPassword"
Send-MailMessage -To $newUserPrincipalName -From "[email protected]" -Subject "Welcome to Our Organization" -Body $welcomeMessage -SmtpServer "smtp.yourdomain.com"
Write-Host "User $newUserDisplayName has been successfully created and provisioned."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.
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.