Streamlining User Onboarding with PowerShell in M365

Welcome to our new series on useful PowerShell scripts that can help enhance your workflow and simplify processes in various technologies! In this installment, were focusing on a script that automates user onboarding in Microsoft 365 (M365). Proper onboarding is crucial for organizations to ensure that new team members have the access and resources they need from day one.
This PowerShell script will create a new user, assign them to predefined groups, and set up essential licenses, streamlining the entire onboarding process!
Lets dive into the steps involved in this script.
### Step 1: Connect to Microsoft 365
Before you can run any commands to manage users in M365, you need to connect to the Microsoft 365 service. This step requires the Azure Active Directory module.
“`powershell

# Import the Azure AD module
Import-Module AzureAD
# Connect to Azure AD
$credential = Get-Credential
Connect-AzureAD -Credential $credential

“`
### Step 2: Create a New User
The next step is to create a new user in Microsoft 365. Youll need to specify properties such as the users display name, user principal name (UPN), and password.
“`powershell

# Define user properties
$DisplayName = "John Doe"
$userPrincipalName = "[email protected]"
$password = "P@ssw0rd!"
# Create new user
New-AzureADUser -DisplayName $DisplayName -UserPrincipalName $userPrincipalName -AccountEnabled $true -MailNickName "johndoe" -PasswordProfile @{ForceChangePasswordNextLogin=$true; Password=$password}

“`
### Step 3: Assign Licenses
After creating the user, we need to assign them a license that grants access to various M365 services.
“`powershell

# License SKU
$LicenseSKU = "yourtenant:ENTERPRISEPACK"  # Example for Microsoft 365 E3
# Assign license to the new user
Set-AzureADUserLicense -ObjectId $userPrincipalName -AddLicenses $LicenseSKU

“`
### Step 4: Add User to Groups
To ensure that the new employee has the necessary permissions and resources, you will want to add them to specific groups.
“`powershell

# Group Object ID for the group you want to add the user to
$groupId = "Your-Group-Object-ID"
# Add user to group
Add-AzureADGroupMember -ObjectId $groupId -RefObjectId (Get-AzureADUser -ObjectId $userPrincipalName).ObjectId

“`
### Final Step: Confirmation
Finally, you can confirm that the user has been created, assigned a license, and added to the appropriate groups.
“`powershell

# Confirm user creation and group membership
Get-AzureADUser -ObjectId $userPrincipalName
Get-AzureADGroupMember -ObjectId $groupId

“`
This script efficiently manages the user onboarding process in Microsoft 365, saving IT teams valuable time.
For more powerful tools to help manage your server environment, check out ServerEngine at [https://serverengine.co](https://serverengine.co) where automation meets efficiency!