Create a Microsoft 365 User With Graph API PowerShell

In this blog post, we will create a user in Microsoft 365 using the Microsoft Graph API PowerShell module.

Microsoft Graph API PowerShell allows us to access Microsoft 365 services programmatically and interact directly with Graph API. The benefit of using the API rather than the Azure AD PowerShell module is that it offers access to more features using less coding.

Authentication and Code

Before you start, make sure you can connect to Graph API using a certificate and have the Directory.ReadWrite.All API permissions via an app registration.

The following PowerShell script will create a new user in Microsoft 365 without a license and set a temporary password.

Import-Module Microsoft.Graph.Users

$params = @{
	accountEnabled = $true
	displayName = "Test"
	mailNickname = "Account"
	userPrincipalName = "testaccount@tenentid.onmicrosoft.com"
	passwordProfile = @{
		forceChangePasswordNextSignIn = $true
		password = "SET PASSWORD"
	}
}

New-MgUser -BodyParameter $params

To check if the user was created, run the following cmdlet

Get-MgUser -all


by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.