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."