Efficient User Onboarding in Microsoft 365 with PowerShell

Onboarding new users in Microsoft 365 can often be a time-consuming process, especially in larger organizations with many new hires. Automating this process can save significant administrative time and reduce the chances of errors. This PowerShell script will help you onboard new users by creating their accounts, assigning licenses, and adding them to specific groups in Microsoft 365.
### Step 1: Install Required PowerShell Module
First, ensure you have the Microsoft Graph PowerShell module installed. This module is essential for executing commands to manage Microsoft 365 resources.
“`powershell

# Install the Microsoft Graph module if it isnt already installed
Install-Module Microsoft.Graph -Force -AllowClobber

“`
### Step 2: Connect to Microsoft 365
Authenticate to your Microsoft 365 tenant using the following command. This will prompt you for your admin credentials.
“`powershell

# Connect to Microsoft 365
Connect-MgGraph -Scopes User.ReadWrite.All, Group.ReadWrite.All, Directory.ReadWrite.All

“`
### Step 3: Define User Attributes
Specify the attributes for the new user you want to create, including their email, display name, password, license, and group ID.
“`powershell

# Define new user attributes
$UserPrincipalName = "[email protected]"  # Replace with the users email address
$DisplayName = "New User"
$Password = "P@ssw0rd!"                          # Ensure this meets the password policy requirements
$LicenseSkuId = "yourtenant:ENTERPRISEPACK"     # Replace with the appropriate SKU ID for licensing
$GroupId = "your-group-id"                       # Replace with the ID of the group to which the user will be added

“`
### Step 4: Create the New User
Use the defined attributes to create a new user account in Microsoft 365.
“`powershell

# Create a new user in Microsoft 365
New-MgUser -AccountEnabled $true `
            -DisplayName $DisplayName `
            -MailNickName $DisplayName `
            -UserPrincipalName $UserPrincipalName `
            -GivenName "New" `
            -Surname "User" `
            -PasswordProfile @{ForceChangePasswordNextSignIn = $true; Password = $Password}

“`
### Step 5: Assign License to the New User
Once the user is created, assign the appropriate license to ensure they have access to Microsoft 365 services.
“`powershell

# Assign license to the new user
Set-MgUserLicense -UserId $UserPrincipalName -AddLicenses $LicenseSkuId

“`
### Step 6: Add User to Security Group
Add the newly created user to a specified security group to grant them necessary permissions.
“`powershell

# Add the new user to a specified security group
Add-MgGroupMember -GroupId $GroupId -UserId $UserPrincipalName

“`
### Conclusion
This PowerShell script automates the onboarding process in Microsoft 365, helping you create user accounts, assign licenses, and manage group memberships more efficiently. By streamlining this process, you can focus on other critical tasks within your organization.
For more powerful server management and automation tools, check out ServerEngine at [https://serverengine.co](https://serverengine.co). Empower your infrastructure with our advanced software solutions!