Microsoft 365 Group User Import Using Graph PowerShell

To add or import Microsoft 365 (M365) users from a CSV file to a Microsoft 365 group using Microsoft Graph PowerShell, you can follow a straightforward process. This involves reading user identifiers from a CSV file and then adding those users to a specified group.


Create the CSV File

First, prepare your CSV file with the necessary data. For this task, you will typically need at least the following headers:

  • UserId: This can be the user's email address, user principal name (UPN), or object ID.
  • GroupId: The object ID of the Microsoft 365 group to which users will be added. This can be a static value if all users are going to the same group, or vary per user if different.

Example CSV File Structure

CSV file for Microsoft 365 User Import with UserId and GroupId headers.

Notes:

  • You can get the Group ID by running the Get-MgGroup cmdlet.
  • You can get the User IDs of the users to be added to the group by running Get-MgUser cmdlet.


Create the Graph PowerShell Script

Create a script to read the CSV file and add users to the specified Microsoft 365 group. The script makes use of Import-Csv to import the users from the CSV file and New-MgGroupMember cmdlet to add the users to the selected Microsoft 365 group.

Graph PowerShell Script for Microsoft 365 Group User Import using CSV file.

You can download the script here: m365-group-user-import.ps1

When you execute the script, you should get the following response:

Graph PowerShell Script for Microsoft 365 Group User Import Output.

How the Script Works?

The Microsoft 365 Group User Import PowerShell script does the following:

  • Establishes connection with Microsoft Graph using GroupMember.Read.All and User.Read.All permissions.
  • Calls the Import-M365UsersToGroup function which takes in the path to your CSV file as the parameter.
  • Next Import-CSV cmdlet is used to read/import the user details from the CSV file.
  • Then the imported user list is looped through and New-MgGroupMember cmdlet is executed for each of those users.
  • The New-MgGroupMember cmdlet accepts -GroupId and -DirectoryObjectId parameter value and adds the imported users to the specified group.

Further Enhancing the Script

Enhancing the script for importing users into Microsoft 365 groups can improve its efficiency, usability, and robustness. Here are some tips on how you might further enhance this PowerShell script:

  • Parameter Validation: Add parameter validation in your script to check the inputs before attempting operations. For example, ensure that the CSV file exists and the format is correct before processing it.
  • Enhancing Microsoft 365 Group User Import with parameter validation.
  • Logging: Implement logging to capture detailed information about the script's execution, which can help in troubleshooting if something goes wrong. You might want to log successful additions as well as errors.
  • Enhancing Microsoft 365 Group User Import with logging for troubleshooting.
  • Error Handling and Recovery: Enhance error handling to not only catch errors but to also attempt recovery if possible. For instance, retry the operation a certain number of times before failing.
  • Input Flexibility: Allow the script to accept parameters directly from the command line or through a configuration file. This makes the script more flexible and easier to integrate into different workflows.
  • Security Enhancements: Ensure that sensitive data such as user IDs and group IDs are handled securely. Implement secure logging and error handling to prevent leakage of sensitive information.

These enhancements can make your script more robust, efficient, and easier to maintain. Depending on your specific needs and environment, you may choose to implement some or all of these improvements.


Related Articles:

Connect to Microsoft 365 Using PowerShell
How to Create Bulk Users in Office 365 Using Graph PowerShell?
Create Microsoft 365 Group Using Microsoft Graph PowerShell
Block Microsoft 365 User Using Microsoft Graph PowerShell
Assign Microsoft 365 License Using Graph PowerShell
Microsoft 365 User Management Using Graph PowerShell
Checking Group Membership in Microsoft 365
Bulk Assign Microsoft 365 License
Find Inactive Users in Microsoft 365
Using Powershell Graph Search Query
Using Powershell Graph Filter Query
Using Where-Object In Graph PowerShell
Using Expand Property In Graph PowerShell
Using Select Object In Graph PowerShell
Using -Contains Operator In Graph PowerShell
Add User to Multiple Microsoft 365 Groups Using Graph PowerShell
Get Microsoft 365 User Location Using Graph PowerShell
Import Microsoft 365 Groups from CSV File Using Graph PowerShell

© m365corner.com. All Rights Reserved. Design by HTML Codex