Microsoft Entra ID, Last Sign In

GuestGuivenchi 105 Reputation points
2025-03-06T23:23:19.0233333+00:00

Good afternoon.

I am a tenant administrator, I need to obtain the last sign in of the users, currently I see that I get the following error, it is worth mentioning that I always use this script and I did get the information but a few days ago I started to receive this error, can you help me to know why the error occurs and share with me an alternative to obtain this information.

Thank you

-----Script-----
#Connect to Microsoft Graph

Connect-MgGraph -Scopes "AuditLog.Read.All","User.Read.All"

#Set the Graph Profile

Select-MgProfile -Name "beta"

#Properties to Retrieve

$Properties = @(

'Id','DisplayName','Mail','UserPrincipalName','UserType', 'AccountEnabled', 'SignInActivity'   

)

#Get All users along with the properties

$AllUsers = Get-MgUser -All -Property $Properties #| Select-Object $Properties

$SigninLogs = @()

ForEach ($User in $AllUsers)

{

$SigninLogs += [PSCustomObject][ordered]@{

        LoginName       = $User.UserPrincipalName

        Email           = $User.Mail

        DisplayName     = $User.DisplayName

        UserType        = $User.UserType

        AccountEnabled  = $User.AccountEnabled

        LastSignIn      = $User.SignInActivity.LastSignInDateTime

}

}

$SigninLogs

#Export Data to CSV

$SigninLogs | Export-Csv -Path "c:\info\SigInUsers.csv" -NoTypeInformation

----Error -----
Get-MgUser : Se han producido uno o varios errores.

En línea: 20 Carácter: 1

  • $AllUsers = Get-MgUser -All -Property $Properties
  • 
        + CategoryInfo          : NotSpecified: (:) [Get-MgUser_List], AggregateException
    
        + FullyQualifiedErrorId : System.AggregateException,Microsoft.Graph.PowerShell.Cmdlets.GetMgUser_List
    
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,882 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Ian Xue-MSFT 41,061 Reputation points Microsoft External Staff
    2025-03-07T07:27:17.1+00:00

    Hi,

    Please check the version of the Microsoft.Graph.Authentication module. There could be a conflict if different versions of the modules are loaded. You can uninstall all the versions of the module then reinstall the latest one.

    Reference:

    https://github.com/microsoftgraph/msgraph-sdk-powershell/issues/1981

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    0 comments No comments

  2. Kavya 490 Reputation points
    2025-03-10T11:23:04.5033333+00:00

    Select-MgProfile -Name "beta" option was deprecated. To use beta cmdlets, you need to install MS Graph beta PowerShell module separately. But, you can get the required last login time with delta version itself.

    You can use this PowerShell script to get users' last login time.

    https://o365reports.com/2023/06/21/microsoft-365-inactive-user-report-ms-graph-powershell/

    The script exports both interactive and non-interactive last sign-in time separately.

    0 comments No comments

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.