Check if users are in Entra ID Recycle Bin

mark terry 85 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

testuser1@test.com

testuser2@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 Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,598 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
22,812 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Harpreet Singh Matharoo 8,306 Reputation points Microsoft Employee
    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 Answers by the question author, which helps users to know the answer solved the author's problem.