Skip to content

It is necessary to clean up the guest accounts of Oamk users in M365, so that they can be taken as guest users in Teams again and files can be shared with them in OneDrive.

Oamk users have been migrated to the University of Oulu’s M365 tenant on May 20, 2024. For some of them, the User Principal Name (UPN) has also changed, but everyone still receives emails with the same email addresses as before. In order for your users to successfully re-share rights to documents to Oamk users and add Oamk users back to those Teams groups where they have previously been as guest members, the administrator of the M365 environment must clean up the old and guest accounts from Azure.

it can be done like this (two options)

1. option: delete individual users

  1. 1st phase, delete user (Tenant Administrator)
    How to create or delete users in Microsoft Entra ID – Microsoft Entra | Microsoft Learn
  2. 2nd phase, permanently delete user (Tenant Administrator)
    Restore or permanently remove recently deleted user – Microsoft Entra | Microsoft Learn
  3. 3rd phase, invite user again to the Teams (User; team owner)
    Add guests to a team in Microsoft Teams – Microsoft Support

2. option: use a script to delete all old account from Oamk’s retired tenant

(NB: The code was updated on May 28, so that it will not remove new guest users added on May 20th or later):
# Connect-MgGraph api with write privileges
Connect-MgGraph -Scopes "User.ReadWrite.All"
# Search all users with userprincipalname containing "_oamk" or "_students.oamk" and "EXT"

$OamkUsers = Get-MgUser -all -filter "userType eq 'Guest'" | Where-Object {$_.userprincipalname -like "**EXT*" -or $_.userprincipalname -like "**EXT*"}
Write-Host "Total usercount:" $OamkUsers.count

$date = Get-Date -Year 2024 -Month 5 -Day 20 -Hour 23 -Minute 59

# Loop through userlist and delete
foreach ($user in $OamkUsers) {
$created = (get-mguser -UserId $user.Id -Property displayname, id, mail, userprincipalname, CreatedDateTime).CreatedDateTime
if($created -lt $date)
Write-Host "Deleting user $($user.userPrincipalName)"
Remove-MgUser -UserId $user.Id

You could also use the $_.userprincipalname for a certain username.

Also, please remove the recently deleted user permanently, as adviced on Microsoft Entra instructions. This can be done by the tenant administration only.

« Back

This article was published in categories English version available, All instructions, Oamk , accessible content. Add the permalink to your favourites.