Check if users are in Entra ID Recycle Bin

mark terry 185 Reputation points
2025-01-09T00:57:08.6433333+00:00

Hi Folks!

I have a script which I am using today to check to see what users are in the Entra ID Recycle Bin and those who are no longer in the Recycle Bin. The script uses the Get-MsolUser -ReturnDeletedUsers command (which is being discontinued). It looks like I will have to start moving to using the Get-MgUser (MS Graph) command, but it looks like there is no -ReturnDeletedUsers switch for Get-MgUser.

The script I currently use is below.

The input file just has the userprincipalname as the header, followed by the users

userprincipalname

******@test.com

******@test.conm

Can someone please help with re-tooling my script so that it works with MS Graph?

Thanks in advance!

# CHECK IF USERS ARE IN THE ENTRA ID RECYCLE BIN AND CREATE RELEVANT FILES

$CSVFile = "D:\Temp\Master-Input-File.csv"
$Users = Import-Csv -Path $CSVFile

Write-Host "Checking if users are in the Entra ID Recycle Bin" -ForegroundColor Green
Write-Host
ForEach ($User in $Users)
{
    If(Get-MsolUser -UserPrincipalName $User.userprincipalname -ReturnDeletedUsers -ErrorAction SilentlyContinue)
    {
        Write-host "$($User.UserPrincipalName) is in the Entra ID Recycle Bin"
        $User.UserPrincipalName | 
        Out-File D:\Temp\Users-in-Recycle-Bin.csv -Append            
    }
    Else
    {
        Write-host "$($User.UserPrincipalName) is not in the Entra ID Recycle Bin" -f Yellow
        $User.UserPrincipalName | 
        Out-File D:\Temp\Users-Not-In-Recycle-Bin.csv -Append
        
    }
}
Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Entra | Microsoft Entra ID
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Harpreet Singh Matharoo 8,416 Reputation points Microsoft Employee Moderator
    2025-01-09T04:41:30.7566667+00:00

    Hello @mark terry ,

    Thank you for your reaching out to Microsoft QnA Platform. I guess you should be able to replace "Get-MsolUser -ReturnDeletedUsers" with "Get-MgDirectoryDeletedItem -Filter "userPrincipalName eq '$($User.userprincipalname)'".

    Hope this will help. Please "Accept the answer" if the information helped you. This will help us and others in the community as well.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.