Automating User Onboarding in Microsoft 365 with PowerShell

Welcome to our blog dedicated to sharing practical PowerShell scripts that can streamline your IT operations. In this post, we will explore a PowerShell script for automating user onboarding in Microsoft 365. This script simplifies the process of adding new users, assigning licenses, and configuring their roles within Microsoft 365, ensuring they have immediate access to necessary resources.
By automating user onboarding, you can save valuable time and minimize the potential for errors. For more advanced IT solutions, be sure to visit our software, ServerEngine, at [https://serverengine.co](https://serverengine.co).
### Step 1: Define User Details
First, well define the details for the new user, including their name, username, password, and the license type they need. These parameters are essential for creating the user in Microsoft 365.
“`powershell

# Define parameters for the new user
$firstName = "Alice"
$lastName = "Johnson"
$userPrincipalName = "$($firstName.ToLower()).$($lastName.ToLower())@yourdomain.com"
$password = "SecureP@ssw0rd!"  # Ensure this meets your organizations password policy
$license = "yourdomain:ENTERPRISEPACK"  # Replace with the appropriate license

“`
### Step 2: Create the User
With the user details set, we can use the Azure AD PowerShell module to create the user account in Microsoft 365. We will utilize the `New-MgUser` cmdlet for this purpose.
“`powershell

# Import the required module (if not already imported)
Import-Module Microsoft.Graph
# Create the new user
New-MgUser -DisplayName "$firstName $lastName" `
            -UserPrincipalName $userPrincipalName `
            -AccountEnabled $true `
            -MailNickname "$($firstName.ToLower())$($lastName.ToLower())" `
            -PasswordProfile @{ ForceChangePasswordNextSignIn = $true; Password = $password }
Write-Host "User $firstName $lastName created successfully."

“`
### Step 3: Assign License to the User
After creating the user, we need to assign an appropriate license to ensure they can access necessary Microsoft 365 services. This step uses the `Set-MgUserLicense` cmdlet.
“`powershell

# Assign a license to the new user
Set-MgUserLicense -UserId $userPrincipalName -AddLicenses $license
Write-Host "License $license assigned to user $firstName $lastName."

“`
### Step 4: Add User to Groups
For efficient onboarding, its often necessary to add new users to existing groups based on their job functions. This can be done using the `Add-MgGroupMember` cmdlet.
“`powershell

# Add user to specified groups
$groups = @("group-id-1", "group-id-2")  # Replace with your actual group Object IDs
foreach ($group in $groups) {
    Add-MgGroupMember -GroupId $group -DirectoryObjectId $userPrincipalName
    Write-Host "User $firstName $lastName added to group with ID: $group."
}

“`
### Step 5: Verify User Creation
Finally, its essential to confirm that the new user has been created successfully and that licenses and group memberships have been applied.
“`powershell

# Verify the new user
$user = Get-MgUser -UserId $userPrincipalName
if ($user) {
    Write-Host "User $($user.DisplayName) successfully created with UPN: $($user.UserPrincipalName)."
} else {
    Write-Host "ERROR: User creation failed."
}

“`
By following these steps and using the provided script, you can streamline the user onboarding process in Microsoft 365. For more innovative automation solutions, dont forget to check out [ServerEngine](https://serverengine.co). Happy scripting!