DEV Community

Asim Baral
Asim Baral

Posted on

How to manage Microsoft 365 Users with PowerShell?

PowerShell is a powerful tool that enables administrators to manage Microsoft 365 (eg: Microsoft 365 Business Basic) users efficiently. Whether you’re creating new users, modifying existing ones, or performing bulk operations, PowerShell provides the necessary commands to handle these tasks quickly and effectively. This guide will walk you through various user management tasks in Microsoft 365 using PowerShell.

Prerequisites
Before you start managing users, ensure you have the following:

a) PowerShell Modules: Install the required modules:

Azure AD for managing Azure Active Directory users.
MSOnline for managing Microsoft Online services.

b) Administrative Privileges: You must have administrative privileges in your Microsoft 365 tenant to execute the necessary commands.

Installing the Required Modules
To install the necessary PowerShell modules, run the following commands:

Install-Module -Name AzureAD
Install-Module -Name MSOnline
Enter fullscreen mode Exit fullscreen mode

Once the modules are installed, you need to connect to your Microsoft 365 tenant:

Connect-AzureAD
Connect-MsolService
Enter fullscreen mode Exit fullscreen mode

This will prompt you to log in using your administrator credentials for Microsoft 365.

Creating a New User
To create a new user, you can use the New-MsolUser cmdlet. This command allows you to specify user details such as their username, display name, first name, last name, and assigned license. Example:
New-MsolUser -UserPrincipalName john@example.com -DisplayName "John Doe" -FirstName "John" -LastName "Doe" -LicenseAssignment contoso:ENTERPRISEPACK

UserPrincipalName (UPN): This specifies the user’s unique login ID (in this case, john@example.com).
DisplayName: This sets the display name for the user (e.g., “John Doe”).
FirstName & LastName: These attributes specify the user’s first and last names.
LicenseAssignment: This assigns the user a specific Microsoft 365 license (e.g., contoso:ENTERPRISEPACK).
The command will create a new user with the specified details and assign them the ENTERPRISEPACK license.

Removing a User
If you need to remove a user from your Microsoft 365 tenant, the Remove-MsolUser cmdlet is used. This command deletes the user and removes their access to Microsoft 365 services. Example:
Remove-MsolUser -UserPrincipalName john@example.com

This command will delete the specified user from the directory.

Modifying User Properties
To modify existing user properties (such as a phone number), you can use the Set-MsolUser cmdlet. This allows you to update specific details for a user. Example:
Set-MsolUser -UserPrincipalName john@example.com -PhoneNumber "123456789"

This command will update the user’s phone number.

Managing Passwords
You can also manage user passwords, including resetting them and requiring a password change upon the next sign-in. The Set-MsolUserPassword cmdlet is used for this task. Example:

Set-MsolUserPassword -UserPrincipalName john@example.com -NewPassword "newpassword123" -ForceChangePassword $true

NewPassword: The new password for the user (e.g., newpassword123).
ForceChangePassword: Set this to $true to require the user to change their password upon their next login.
This command resets the user’s password and forces them to change it the next time they sign in.

PowerShell is an essential tool for managing Microsoft 365 users, allowing administrators to automate and simplify user management tasks. From creating new users to modifying user properties and managing passwords, PowerShell provides all the necessary cmdlets for managing a Microsoft 365 environment. Always remember to test your scripts in a non-production environment before applying them to your live tenant. This ensures that any issues or mistakes can be caught without affecting your users.

Top comments (0)