Is there any PowerShell script to get the Audit and Sign-in logs from Azure AD tenant via PowerShell or we can create a scheduled script to run on particular period in day or week?

Vinod Survase 4,736 Reputation points
2023-09-22T13:54:26.7+00:00

Is there any PowerShell script to get the Audit and Sign-in logs from Azure AD tenant via PowerShell or we can create a scheduled script to run on particular period in day or week?

Microsoft 365
Microsoft 365
Formerly Office 365, is a line of subscription services offered by Microsoft which adds to and includes the Microsoft Office product line.
4,775 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
21,469 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Brian Zarb 1,650 Reputation points
    2023-09-22T15:27:13.02+00:00

    Yes, you can utilize Azure AD PowerShell module to fetch Audit and Sign-in logs from Azure AD tenant. Below are the steps and a basic example of how you can do this:


    To Get Audit and Sign-in Logs For Audit logs use the following snippet: (after you connect to Azure)

    Get-AzureADAuditDirectoryLogs
    

    For Sign-in logs, you may need to use AzureAD V2 module and the Get-AzureADSignInLogs cmdlet.


    Schedule the script You can use Windows Task Scheduler to run your PowerShell script at a specific time of day or week. Here is an example script that gets today's audit logs and sign-in logs:

    # Connect to Azure AD
    Connect-AzureAD
    # Get Today's date
    $today = Get-Date
    # Fetch Audit Logs
    $auditLogs = Get-AzureADAuditDirectoryLogs -Filter "ActivityDateTime ge $today"
    # Fetch Sign-in Logs (Requires AzureAD V2 module)
    $signInLogs = Get-AzureADSignInLogs -Filter "CreatedDateTime ge $today"
    # Process or Export logs as needed
    $auditLogs | Export-Csv -Path "C:\Path\To\Export\AuditLogs.csv" -NoTypeInformation
    $signInLogs | Export-Csv -Path "C:\Path\To\Export\SignInLogs.csv" -NoTypeInformation
    

  2. Tushar Kumar 3,321 Reputation points MVP
    2023-09-22T15:27:51.1166667+00:00

    HiVinod Survase,

    Surely you can do it, You can create Automation acount and schedule it according to your need .

    Get-AzureADAuditSignInLogs -All $true | Export-CSV -Path "C:\AzureADAuditSignInLogs.csv" -NoTypeInformation
    
    
    

    https://morgantechspace.com/2021/11/export-azure-ad-sign-in-audit-logs-using-powershell.html

    You can take a look at above link for more details.

    Please Click "Accept as answer" if this helps.


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.