Automating User Onboarding with Microsoft Entra ID and PowerShell
User onboarding is a critical process that can often be time-consuming and prone to error. By automating the onboarding process, organizations can ensure new hires are set up quickly and efficiently. This PowerShell script is designed to create new user accounts in Microsoft Entra ID, assign roles, and ensure necessary settings are configured.
This script will:
1. Create a new user in Microsoft Entra ID with specified credentials.
2. Assign the user to default groups for access.
3. Send a welcome email with account details and instructions.
By implementing this script, IT administrators can streamline the onboarding process, allowing new users to become productive quicker.
# Import the Microsoft Graph module Install-Module Microsoft.Graph -Scope CurrentUser -AllowClobber -Force Import-Module Microsoft.Graph # Connect to Microsoft Graph Connect-MgGraph -Scopes 'User.ReadWrite.All', 'Group.ReadWrite.All' # Parameters for the new user $newUserPrincipalName = "[email protected]" $newUserDisplayName = "New User" $newUserPassword = "P@ssword123!" # Generate a secure password $groupId = "your-default-group-id" # Create the new user $newUser = New-MgUser -UserPrincipalName $newUserPrincipalName -DisplayName $newUserDisplayName -AccountEnabled $true -MailNickname "newuser" -PasswordProfile @{ ForceChangePasswordNextSignIn = $true Password = $newUserPassword } # Assign the user to the default group Add-MgGroupMember -GroupId $groupId -DirectoryObjectId $newUser.Id # Optional: Send a welcome email $welcomeMessage = "Welcome $newUserDisplayName! Your account has been created. Log in using: Username: $newUserPrincipalName Password: $newUserPassword" Send-MailMessage -To $newUserPrincipalName -From "[email protected]" -Subject "Welcome to Our Organization" -Body $welcomeMessage -SmtpServer "smtp.yourdomain.com" Write-Host "User $newUserDisplayName has been successfully created and provisioned."