Streamlining User Onboarding in Microsoft Entra ID with PowerShell
Efficient user onboarding is essential for organizations to integrate new employees smoothly while ensuring compliance and security. This PowerShell script automates the process of creating user accounts in Microsoft Entra ID and assigning roles, which helps streamline the onboarding experience for both IT administrators and new users. This script will: 1. Connect to Microsoft Entra ID using the Microsoft Graph API. 2. Create a new user with specified attributes. 3. Assign the user to a designated role or group. By using this script, organizations can enhance the efficiency of their onboarding processes, allowing HR and IT teams to focus on other critical tasks.
# Install the Microsoft Graph module if it's not already installed
if (-Not (Get-Module -ListAvailable -Name Microsoft.Graph)) {
Install-Module -Name Microsoft.Graph -Scope CurrentUser -AllowClobber
}
# Connect to Microsoft Graph
Connect-MgGraph -Scopes 'User.ReadWrite.All', 'Group.ReadWrite.All'
# Define the parameters for the new user account
$userPrincipalName = "[email protected]" # Replace with the user's email
$displayName = "New User"
$password = "SecureP@ssword!" # Use a strong password
$mailNickname = "newuser"
$groupId = "your-group-id" # Replace with the ID of the group to assign
# Create the new user account
$newUser = New-MgUser -UserPrincipalName $userPrincipalName -DisplayName $displayName -AccountEnabled $true -MailNickname $mailNickname -PasswordProfile @{
ForceChangePasswordNextSignIn = $true
Password = $password
}
# Assign the user to a group
Add-MgGroupMember -GroupId $groupId -DirectoryObjectId $newUser.Id
# Output success message
Write-Host "User account '$($newUser.DisplayName)' created and assigned to group."
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "Onboarding process completed."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.