Simplifying User Onboarding in Microsoft Entra ID with PowerShell

Efficient user onboarding is crucial for organizations to integrate new hires effectively while maintaining security and access control. This PowerShell script automates the onboarding process for new users in Microsoft Entra ID, simplifying account creation and group assignment.
This script will:
1. Connect to Microsoft Entra ID.
2. Create a new user account with specified details.
3. Assign the user to relevant groups for required access.
By automating these tasks, IT administrators can streamline the onboarding process, reduce errors, and enhance the overall efficiency of new user integration.

# Import the 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@ssw0rd123!"  # Use a secure password
    }
}
# Create the new user account
$createdUser = New-MgUser -BodyParameter $newUser
# Assign the user to a default group
$groupId = "your-group-id"  # Replace with your group ID
Add-MgGroupMember -GroupId $groupId -DirectoryObjectId $createdUser.Id
# Output results
Write-Host "User '$($createdUser.DisplayName)' created successfully with UPN: $($createdUser.UserPrincipalName)"
# Disconnect from Microsoft Graph
Disconnect-MgGraph
Write-Host "User onboarding process completed."