Performing bulk operations in Microsoft 365 with PowerShell

Performing bulk operations in Microsoft 365 with PowerShell

Performing bulk operations in Microsoft 365 with PowerShell, such as bulk adding users or modifying attributes, can be a very useful and efficient way to manage your organization's users and resources. PowerShell is a powerful command-line tool that allows you to automate repetitive tasks and perform bulk operations quickly and easily.

In this article, we will cover the basics of performing bulk operations in Microsoft 365 with PowerShell. We will cover how to bulk add users and modify attributes, including user properties and group membership.



Bulk Adding Users


Adding a single user to Microsoft 365 can be done through the admin portal, but when it comes to adding multiple users, PowerShell is a more efficient and time-saving method. Here are the steps to add multiple users using PowerShell:


1. Connect to Microsoft 365: 

The first step is to connect to your Microsoft 365 account using PowerShell. To do this, open PowerShell and run the following command:

  1. Connect-MsolService 

This command will prompt you to enter your admin credentials.


2. Create a CSV file: 

Next, you will need to create a CSV file with the user information you want to add. The CSV file should include the following headers:

  1. UserPrincipalName, DisplayName, FirstName, LastName, Password, UsageLocation 

Here is an example of what the CSV file might look like:


UserPrincipalName,DisplayName,FirstName,LastName,Password,UsageLocation john.doe@contoso.com,John Doe,John,Doe,Password123,US jane.doe@contoso.com,Jane Doe,Jane,Doe,Password123,US 


3. Import the CSV file: 

Once you have created the CSV file, you can import it into PowerShell using the following command:

  1. $users = Import-Csv -Path C:\path\to\file.csv 


4. Create the users: 


Finally, you can create the users by running the following command:

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

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



Bulk Modifying User Attributes


In addition to adding multiple users at once, PowerShell can also be used to modify attributes for multiple users. Here are the steps to modify user attributes using PowerShell:


1. Connect to Microsoft 365: 

As with adding users, the first step is to connect to your Microsoft 365 account using PowerShell.


2. Create a CSV file: 

Next, you will need to create a CSV file with the user information you want to modify. The CSV file should include the following headers:

UserPrincipalName, AttributeName, AttributeValue 

Here is an example of what the CSV file might look like:

UserPrincipalName,AttributeName,AttributeValue john.doe@contoso.com,Department,IT jane.doe@contoso.com,Department,HR 


3. Import the CSV file: 

Once you have created the CSV file, you can import it into PowerShell using the following command:

  1. $users = Import-Csv -Path C:\path\to\file.csv 


4. Modify the attributes: 

Finally, you can modify the user attributes by running the following command:


  1. foreach ($user in $users) { Set-MsolUser -UserPrincipalName $user.UserPrincipalName - $user.AttributeName $user.AttributeValue } 

This command will modify the attributes specified in the CSV file for each user.



Bulk Modifying Group Membership


Another common task in Microsoft 365 administration is managing group membership. PowerShell can be used to bulk modify group membership for multiple users. Here are the steps to modify group membership using PowerShell:


1. Connect to Microsoft 365: 

As with the previous examples, the first step is to connect to your Microsoft 365 account using PowerShell.


2. Create a CSV file: 

Next, you will need to create a CSV file with the user information and the groups you want to modify. The CSV file should include the following headers:

UserPrincipalName, GroupName, Action 

The Action column should contain either "Add" or "Remove" to indicate whether the user should be added to or removed from the group. Here is an example of what the CSV file might look like:


UserPrincipalName,GroupName,Action john.doe@contoso.com,IT Group,Add jane.doe@contoso.com,HR Group,Remove 


3. Import the CSV file: 

Once you have created the CSV file, you can import it into PowerShell using the following command:


  1. $memberships = Import-Csv -Path C:\path\to\file.csv 

4. Modify the group membership: 

Finally, you can modify the group membership by running the following command:


  1. foreach ($membership in $memberships) { if ($membership.Action -eq "Add") { Add-MsolGroupMember -GroupObjectId (Get-MsolGroup -SearchString $membership.GroupName).ObjectId -GroupMemberObjectId (Get-MsolUser -UserPrincipalName $membership.UserPrincipalName).ObjectId } elseif ($membership.Action -eq "Remove") { Remove-MsolGroupMember -GroupObjectId (Get-MsolGroup -SearchString $membership.GroupName).ObjectId -GroupMemberObjectId (Get-MsolUser -UserPrincipalName $membership.UserPrincipalName).ObjectId } } 

This command will add or remove the specified users from the specified groups based on the action specified in the CSV file.



Conclusion


In conclusion, PowerShell is a powerful tool that can be used to perform bulk operations in Microsoft 365. Whether you need to add multiple users, modify user attributes, or manage group membership, PowerShell can help you automate these tasks and save time. By following the steps outlined in this article, you can quickly and easily perform bulk operations in Microsoft 365 with PowerShell.