Sunday, 19 September 2021

How to Get Last Logon Report from Azure AD Using PowerShell.

 Azure AD Last Logon Report


Connect-AzureAD

 

Connect-MsolService 


For All User

 

 

$Users = Get-MsolUser -all

$Headers = "DisplayName`tUserPrincipalName`tLicense`tLastLogon" >>C:\Temp\Users.txt

ForEach ($User in $Users)

    {

    $UPN = $User.UserPrincipalName

    $LoginTime = Get-AzureAdAuditSigninLogs -top 1 -filter "userprincipalname eq '$UPN'" | select CreatedDateTime

    $NewLine = $User.DisplayName + "`t" + $User.UserPrincipalName + "`t" + $User.Licenses.AccountSkuId + "`t" + $LoginTime.CreatedDateTime

    $NewLine >>C:\Temp\Users.txt

    } 


From CSV File.

 

File Format






$Users= Import-CSV -Path C:\temp\output.CSV |  ForEach-Object {get-MsolUser -UserPrincipalName $_.UserPrincipalName} 

$Headers = "DisplayName`tUserPrincipalName`tLicense`tLastLogon" >>C:\Temp\Users.txt

 ForEach ($User in $Users)

    {

    $UPN = $User.UserPrincipalName

    $LoginTime = Get-AzureAdAuditSigninLogs -top 1 -filter "userprincipalname eq '$UPN'" | select CreatedDateTime

    $NewLine = $User.DisplayName + "`t" + $User.UserPrincipalName + "`t" + $User.isLicensed + "`t" + $LoginTime.CreatedDateTime

    $NewLine >>C:\Temp\Users.txt

    }





No comments:

Post a Comment