Automating User Onboarding in Microsoft Entra ID with PowerShell
Onboarding new users efficiently is essential for any organization. This PowerShell script simplifies the onboarding process for new employees by automating user creation and assigning basic roles in Microsoft Entra ID. With this script, IT administrators can ensure that new hires are set up with minimal manual intervention, speeding up the time it takes to get them productive. This script will: 1. Connect to Microsoft Entra ID. 2. Create a new user with specified attributes. 3. Assign the user to a predefined role or group. By utilizing this script, organizations can streamline their onboarding processes, reduce errors, and ensure that all new users receive the appropriate access from day one.
# Import Microsoft Graph module if 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 parameters for the new user
$newUser = @{
UserPrincipalName = "[email protected]"
DisplayName = "New User"
MailNickname = "newuser"
AccountEnabled = $true
PasswordProfile = @{
ForceChangePasswordNextSignIn = $true
Password = "P@ssw0rd!" # Consider generating a stronger password
}
}
# Create the new user
$createdUser = New-MgUser -BodyParameter $newUser
# Assign the user to a group (optional)
$groupId = "your-group-id" # Replace with the target group ID
Add-MgGroupMember -GroupId $groupId -DirectoryObjectId $createdUser.Id
# Output the results
Write-Host "User '$($createdUser.DisplayName)' created successfully with UPN: $($createdUser.UserPrincipalName)"
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "User 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.