← script library

Streamlining User Onboarding in Microsoft Entra ID with PowerShell

Microsoft 365 & Entra IDJanuary 4, 2025

In this article, we will focus on streamlining the user onboarding process in Microsoft Entra ID. Automating this process can save time and reduce human error, ensuring that new employees have immediate access to necessary resources. This PowerShell script will guide you through creating a new user in Microsoft Entra ID, assigning them to appropriate groups, and confirming successful creation.

powershell
# Define parameters for the new user
$firstName = "Jane"
$lastName = "Smith"
$userPrincipalName = "$($firstName.ToLower()).$($lastName.ToLower())@yourdomain.com"
$password = "P@ssword123"
$displayName = "$firstName $lastName"

# Create the new user
New-AzureADUser -DisplayName $displayName -PasswordProfile @{ ForceChangePasswordNextLogin = $true; Password = $password } `
                 -UserPrincipalName $userPrincipalName -GivenName $firstName -Surname $lastName `
                 -AccountEnabled $true
Write-Host "User $displayName created successfully."

# Assign user to groups
$groupIds = @("group-id-1", "group-id-2")  # Replace with actual group Object IDs
foreach ($groupId in $groupIds) {
    Add-AzureADGroupMember -ObjectId $groupId -RefObjectId $userPrincipalName
    Write-Host "User $displayName added to group with ID: $groupId"
}

# Verify user creation
$user = Get-AzureADUser -ObjectId $userPrincipalName
if ($user) {
    Write-Host "User $($user.DisplayName) exists with UPN: $($user.UserPrincipalName)"
} else {
    Write-Host "ERROR: User was not created successfully."
}

# Summary of actions taken
Write-Host "User onboarding for $displayName completed successfully!"
Write-Host "Assigned to following groups: $($groupIds -join , )"

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.