Overview
This procedure outlines the steps to import guest users into Microsoft Entra ID (formerly Azure AD) using a PowerShell script that leverages the Microsoft Graph API.
Unlike the standard guest invitation process, this script enhances user profiles by automatically setting additional attributes, such as:
- Mobile Phone
- Company Name
- Usage Location (defaults to Belgium)
Key Benefits of this Automated Process
- Saves time – No manual user entry required.
- Ensures consistency – Standardized name formatting (DUPONT Jean-Claude).
- Enhances guest user management – Additional attributes ensure proper user records.
- Avoids duplicate users – The script checks if a user exists before adding them.
Prerequisites
1. Install Microsoft Graph PowerShell SDK
If not installed, open PowerShell as Administrator and run:
This ensures access to the Microsoft Graph API commands.
2. Set PowerShell Execution Policy
Allow script execution for the current session:
This prevents security restrictions from blocking the script.
3. Required Permissions in Azure AD
Ensure your Azure AD account has the following permissions:
- User.Invite.All → To invite guest users.
- User.ReadWrite.All → To update user properties (e.g., phone, company, location).
You can check your permissions in the Microsoft Entra Admin Center.
CSV File Structure (Including Additional Attributes)
The script reads a CSV file containing user information. Ensure it follows this format:
Example:
Notes:
- LastName → Will be automatically converted to UPPERCASE.
- FirstName → Will be formatted correctly, preserving hyphens (Jean-Claude).
- MobilePhone → Optional but will be added if available.
- Company → Optional but will be added if provided.
You can also add other attributes if you have more requirements, but think to also adapt the script in this case.
Save the file as:
The script
Running the Script
1. Open PowerShell and navigate to the script location:
2. Run the script:
3. Authenticate to Microsoft Graph when prompted.
- A sign-in window will appear.
- Use an account with User.Invite.All and User.ReadWrite.All permissions.
The script will automatically:
- Check if each guest user already exists.
- Invite new users without sending an email invitation.
- Apply correct name formatting (e.g., "DUPONT Jean-Claude").
- Add additional attributes (MobilePhone, Company, Usage Location).
Expected Output
During execution, PowerShell will display messages indicating the progress:
- Guest user invited: DUPONT Jean-Claude (
This email address is being protected from spambots. You need JavaScript enabled to view it. ) - Updated guest user details (MobilePhone, Company, Usage Location)
- User
This email address is being protected from spambots. You need JavaScript enabled to view it. already exists. (Skipped) - Error inviting/updating user:
This email address is being protected from spambots. You need JavaScript enabled to view it. (If something fails)
After the script completes, all new guest users will be added to Microsoft Entra ID with enriched profile details.
Verifying Imported Users
1. Go to Microsoft Entra Admin Center
2. Check the guest users list
- Navigate to Users > External Identities
- Filter by Guest users
3. Verify user details
- Click on a guest user to check:
- First Name & Last Name (Correct format)
- Mobile Phone (If available)
- Company Name (If provided)
- Usage Location (Set to Belgium)
Troubleshooting
1. Error: "Could not load file or assembly 'Microsoft.Graph.Authentication'"
Solution: Reinstall the Microsoft Graph module:
Restart PowerShell and try again.
2. Error: "The domain portion of the userPrincipalName property is invalid."
Solution: Ensure the script is using New-MgInvitation, not New-MgUser, since guests must be invited.
3. Error: "PipelineStoppedException: The pipeline has been stopped."
Solution: Likely due to authentication failure. Ensure your account has the required permissions.
4. Error: "CSV file not found."
Solution: Verify that the CSV file exists at the specified path:
Conclusion
This script streamlines guest user onboarding in Microsoft Entra ID, ensuring:
- Consistent formatting of names.
- Automatic assignment of additional attributes (phone, company, location).
- Duplicate prevention by checking existing users.