M365 User Management – Microsoft Graph

Ensure the correct module is imported and connect to MgGraph with the right permissions:

Import-Module Microsoft.Graph.Identity.DirectoryManagement

Connect-MgGraph -Scopes 'User.ReadWrite.All, Directory.ReadWrite.All'

Following the guidance from Create user accounts in Microsoft 365 – Training | Microsoft Learn

$PasswordProfile = @{ Password = 'user password' }
New-MgUser -UserPrincipalName username@domainname –DisplayName 'Firstname Lastname' –GivenName 'Firstname' –Surname 'Lastname' -PasswordProfile $PasswordProfile -AccountEnabled -MailNickName 'email alias'

I found that there was a bug with the -Surname parameter with my versions of PowerShell and MgGraph:

  • PowerShell 7.5.0 (running on Windows 11)
  • 2.26.0

The error I was receiving was:

New-MgUser_CreateExpanded: Resource '' does not exist or one of its queried reference-property objects are not present.

Status: 404 (NotFound)
ErrorCode: Request_ResourceNotFound

Once I removed -Surname from the user creation command it then created the user.

New-MgUser -UserPrincipalName username@domainname –DisplayName 'Firstname Lastname' –GivenName 'Firstname' -PasswordProfile $PasswordProfile -AccountEnabled -MailNickName 'email alias'

Comments

Leave a Reply

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