This guide demonstrates how to use the Invoke-MgGraphRequest cmdlet in Microsoft Graph PowerShell to create users in Microsoft 365. Learn how to construct custom requests, define user properties, and handle API responses with practical examples.
Creating users in Microsoft 365 is a common administrative task. While there are dedicated cmdlets for user creation like New-MgUser, the Invoke-MgGraphRequest cmdlet provides a flexible alternative for scenarios where you might need to directly interact with Microsoft Graph API endpoints. This article will walk you through using Invoke-MgGraphRequest specifically for user creation, demonstrating various ways to create users with different levels of detail, including reading data from a CSV file.
Invoke-MgGraphRequest -Method POST -Uri "https://graph.microsoft.com/v1.0/users" -Body $Body
The following script creates a user with minimal required properties:
$Body = @{
accountEnabled = $true
displayName = "John Doe"
mailNickname = "johndoe"
userPrincipalName = "johndoe@yourdomain.com"
passwordProfile = @{
forceChangePasswordNextSignIn = $true
password = "P@ssw0rd!"
}
}
Invoke-MgGraphRequest -Method POST -Uri "https://graph.microsoft.com/v1.0/users" -Body $Body
This example includes additional properties such as givenName, surname, jobTitle, and department to provide more detailed user information:
$Body = @{
accountEnabled = $true
displayName = "Jane Smith"
mailNickname = "janesmith"
userPrincipalName = "janesmith@yourdomain.com"
givenName = "Jane"
surname = "Smith"
jobTitle = "Marketing Manager"
department = "Marketing"
passwordProfile = @{
forceChangePasswordNextSignIn = $true
password = "P@ssw0rd!"
}
}
Invoke-MgGraphRequest -Method POST -Uri "https://graph.microsoft.com/v1.0/users" -Body $Body
This script demonstrates how to create multiple users by looping through an array of user hashtables:
$Users = @(
@{
accountEnabled = $true
displayName = "Alice Johnson"
mailNickname = "alicejohnson"
userPrincipalName = "alicejohnson@yourdomain.com"
passwordProfile = @{
forceChangePasswordNextSignIn = $true
password = "P@ssw0rd!"
}
}
@{
accountEnabled = $true
displayName = "Bob Roberts"
mailNickname = "bobroberts"
userPrincipalName = "bobroberts@yourdomain.com"
passwordProfile = @{
forceChangePasswordNextSignIn = $true
password = "P@ssw0rd!"
}
}
)
foreach ($User in $Users) {
Invoke-MgGraphRequest -Method POST -Uri "https://graph.microsoft.com/v1.0/users" -Body $User
}
The CSV file should have the following structure:
DisplayName,MailNickname,UserPrincipalName,Password
Alice Johnson,alicejohnson,alicejohnson@yourdomain.com,P@ssw0rd!
Bob Roberts,bobroberts,bobroberts@yourdomain.com,P@ssw0rd!
This script reads user data from a CSV file and creates users based on the information provided in the file:
$CSVFilePath = "C:\UsersToCreate.csv"
$Users = Import-Csv -Path $CSVFilePath
foreach ($User in $Users) {
$Body = @{
accountEnabled = $true
displayName = $User.DisplayName
mailNickname = $User.MailNickname
userPrincipalName = $User.UserPrincipalName
passwordProfile = @{
forceChangePasswordNextSignIn = $true
password = $User.Password
}
}
Invoke-MgGraphRequest -Method POST -Uri "https://graph.microsoft.com/v1.0/users" -Body $Body
}
Cause: Insufficient permissions.
Solution: Ensure that the authenticated user has the necessary permissions such as User.ReadWrite.All.
Cause: Incorrect or missing properties in the request body.
Solution: Verify that all required properties are correctly specified and that the JSON structure is valid.
Cause: Using unsupported parameters or incorrect URI.
Solution: Review the Microsoft Graph documentation to ensure you are using supported parameters and correct URI formats.
Cause: -BodyParameter payload is not supported for Invoke-MgGraphRequest cmdlet.
Solution: Just use -Body instead of -BodyParameter and the cmdlet will work.
1. What is Invoke-MgGraphRequest used for?
Invoke-MgGraphRequest is a Microsoft Graph PowerShell cmdlet used to make custom REST API calls to Microsoft Graph, enabling operations like creating users, updating resources, and more.
2. How can I verify if a user was created successfully?
You can verify by retrieving the user’s details using their UserPrincipalName:
Get-MgUser -UserId "johndoe@domain.com"
3. How can I create a user using Invoke-MgGraphRequest?
Use the following script to create a new user:
$Body = @{
accountEnabled = $true
displayName = "John Doe"
mailNickname = "johndoe"
userPrincipalName = "johndoe@domain.com"
passwordProfile = @{
forceChangePasswordNextSignIn = $true
password = "Password123!"
}
}
Invoke-MgGraphRequest -Method POST -Uri "https://graph.microsoft.com/v1.0/users" -Body ($Body | ConvertTo-Json -Depth 10)
4. What permissions are required to create users using Invoke-MgGraphRequest?
You need the User.ReadWrite.All or Directory.ReadWrite.All permission in Microsoft Graph PowerShell. Ensure these permissions are granted in Azure AD.
The Invoke-MgGraphRequest cmdlet offers flexibility when creating users in Microsoft 365, especially when dealing with complex scenarios or when standard cmdlets fall short. By following the examples and tips provided, you can efficiently create users with varying levels of detail, leveraging the full power of Microsoft Graph API.
Remember to handle errors appropriately and ensure your API calls are well-structured to avoid common pitfalls. With practice, this method can become a powerful addition to your M365 management toolkit.
© m365corner.com. All Rights Reserved. Design by HTML Codex