← script library

Efficient User Onboarding with PowerShell

Users & AccountsDecember 23, 2024

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. Below is a sample PowerShell script that creates a new user and assigns a license:

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

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.