Efficient User Onboarding with PowerShell

Welcome to our PowerShell script series! Today, we will explore a script designed to automate the user onboarding process in Microsoft Entra ID. Automating user creation can save your IT team valuable time and ensure that new employees have the necessary access and resources from their first day.
By integrating this script with ServerEngine, found at https://serverengine.co, you can streamline your onboarding procedures and enhance operational efficiency. Below is a sample PowerShell script that creates a new user and assigns a license:

# Define new user information
$firstName = "Michael"
$lastName = "Johnson"
$userPrincipalName = "$($firstName).$($lastName)@yourdomain.com"
$password = "SecurePassword123!"
# Create the new user in Microsoft Entra ID
New-MgUser -AccountEnabled $true -DisplayName "$firstName $lastName" `
    -MailNickName $firstName -UserPrincipalName $userPrincipalName `
    -GivenName $firstName -Surname $lastName `
    -PasswordProfile @{ForceChangePasswordNextSignIn = $true; Password = $password}
# Assign a license (replace with actual SKU ID)
Set-MgUserLicense -UserId $userPrincipalName -AddLicenses @('your-sku-id')
# Add the user to a security group (replace with actual group ID)
Add-MgGroupMember -GroupId "your-group-id" -UserId $userPrincipalName
Write-Host "User $firstName $lastName has been successfully onboarded."