How to automate user management in Microsoft 365 with PowerShell

How to automate user management in Microsoft 365 with PowerShell


Introduction:


Managing users in Microsoft 365 can be a time-consuming task, especially for larger organizations. With PowerShell, however, you can automate many of the tasks involved in user management, making the process faster and more efficient. In this article, we will discuss how to automate user management in Microsoft 365 using PowerShell.


Prerequisites:


Before we begin, you will need to ensure that you have the following:

  • Access to a Microsoft 365 tenant as a global administrator.
  • PowerShell installed on your computer.
  • Azure Active Directory PowerShell module installed.


Automating user creation:


To automate the creation of new users in Microsoft 365 using PowerShell, you can use a script that creates users in bulk. Follow the steps below to create a new user using PowerShell:


1. Connect to Microsoft 365 using PowerShell: 


Open PowerShell on your computer and run the following command to connect to Microsoft 365:


  1. Connect-MsolService


Enter your global administrator credentials when prompted.


2. Create a CSV file: 


Create a CSV file with the following columns: DisplayName, UserPrincipalName, FirstName, LastName, Password, and UsageLocation. Add the user details for each user you want to create.


3. Import the CSV file: 


Run the following command to import the CSV file into PowerShell:


  1. $users = Import-Csv -Path "C:\Users.csv"


Replace "C:\Users.csv" with the path and filename of your CSV file.


4. Create the users: 


Run the following command to create the users in Microsoft 365:


  1. foreach ($user in $users) {
  2. New-MsolUser -DisplayName $user.DisplayName -UserPrincipalName $user.UserPrincipalName -FirstName $user.FirstName -LastName $user.LastName -Password $user.Password -UsageLocation $user.UsageLocation
  3. }


This command will create a new user for each row in the CSV file.


Automating user deletion:


To automate the deletion of users in Microsoft 365 using PowerShell, you can use a script that deletes users in bulk. Follow the steps below to delete a user using PowerShell:


1. Connect to Microsoft 365 using PowerShell: 


Open PowerShell on your computer and run the following command to connect to Microsoft 365:


  1. Connect-MsolService


Enter your global administrator credentials when prompted.


2. Create a CSV file: 


Create a CSV file with the following column: UserPrincipalName. Add the user details for each user you want to delete.


3. Import the CSV file: 


Run the following command to import the CSV file into PowerShell:


  1. $users = Import-Csv -Path "C:\Users.csv"


Replace "C:\Users.csv" with the path and filename of your CSV file.


4. Delete the users: 


Run the following command to delete the users in Microsoft 365:


  1. foreach ($user in $users) {
  2. Remove-MsolUser -UserPrincipalName $user.UserPrincipalName -Force
  3. }

This command will delete each user listed in the CSV file.



Automating user license assignment:


To automate the assignment of licenses to users in Microsoft 365 using PowerShell, you can use a script that assigns licenses in bulk. Follow the steps below to assign licenses to a user using PowerShell:


1. Connect to Microsoft 365 using PowerShell: 


Open PowerShell on your computer and run the following command to connect to Microsoft 365:


  1. Connect-MsolService


Enter your global administrator credentials when prompted.


2. Create a CSV file: 


Create a CSV file with the following columns: UserPrincipalName and LicenseSKU. Add the user details and license SKUs for each user you want to assign licenses to.


3. Import the CSV file: 


Run the following command to import the CSV file into PowerShell:


  1. $users = Import-Csv -Path "C:\Users.csv"


4. Assign the licenses: 


Run the following command to assign the licenses to users in Microsoft 365:


  1. foreach ($user in $users) {
  2. Set-MsolUserLicense -UserPrincipalName $user.UserPrincipalName -AddLicenses $user.LicenseSKU
  3. }


This command will assign the specified licenses to each user listed in the CSV file.



Automating user password reset:


To automate the password reset process for users in Microsoft 365 using PowerShell, you can use a script that resets passwords in bulk. Follow the steps below to reset a user's password using PowerShell:


1. Connect to Microsoft 365 using PowerShell: 


Open PowerShell on your computer and run the following command to connect to Microsoft 365:


  1. Connect-MsolService


Enter your global administrator credentials when prompted.


2. Create a CSV file: 


Create a CSV file with the following columns: UserPrincipalName and Password. Add the user details and new passwords for each user you want to reset passwords for.


3. Import the CSV file: 


Run the following command to import the CSV file into PowerShell:


  1. $users = Import-Csv -Path "C:\Users.csv"


Replace "C:\Users.csv" with the path and filename of your CSV file.


4. Reset the passwords: 


Run the following command to reset the passwords for users in Microsoft 365:


  1. foreach ($user in $users) {
  2. Set-MsolUserPassword -UserPrincipalName $user.UserPrincipalName -NewPassword $user.Password -ForceChangePassword $true
  3. }


This command will reset the password for each user listed in the CSV file.



Conclusion:


Automating user management in Microsoft 365 using PowerShell can save you time and effort. By following the steps outlined in this article, you can easily automate the creation, deletion, license assignment, and password reset processes for users in your organization. PowerShell offers a powerful and efficient way to manage users in Microsoft 365, and it is a skill that can greatly benefit any IT professional working with Microsoft 365.


    • Related Articles

    • Deep Dive into Microsoft 365 PowerShell Scripting

      Microsoft 365 lets administrators manage user accounts, licenses, settings, and more online via a web-based administrative console. Although the console provides a user-friendly interface for managing Microsoft 365, it can be complex when it comes to ...
    • Microsoft 365 Exchange Online - Shared Mailboxes & PowerShell

      Introduction Overview of Shared Mailboxes A shared mailbox in Microsoft 365 Exchange Online allows multiple users to access and manage emails, contacts, and calendar events from a single mailbox. It is commonly used by teams that need to collaborate ...
    • Microsoft 365 Exchange online - Shared Mailboxes & PowerShell

      Introduction Overview of Shared Mailboxes A shared mailbox in Microsoft 365 Exchange Online allows multiple users to access and manage emails, contacts, and calendar events from a single mailbox. It is commonly used by teams that need to collaborate ...
    • Understanding Microsoft 365 Authentication and Identity Management

      Introduction In today's digital world, security is of utmost importance. Businesses and organizations store and exchange sensitive data that can be harmful if it falls into the wrong hands. Microsoft 365 offers various security features to protect ...
    • Managing Microsoft 365 SharePoint sites and content with PowerShell

      Managing Microsoft 365 SharePoint sites and content can be a challenging task, especially when dealing with large amounts of data. To simplify this process, administrators can take advantage of PowerShell scripting to automate various management ...