{"id":225,"date":"2025-08-14T03:55:15","date_gmt":"2025-08-14T02:55:15","guid":{"rendered":"https:\/\/www.stevenhodson.com\/?p=225"},"modified":"2025-08-14T04:02:55","modified_gmt":"2025-08-14T03:02:55","slug":"powershell-download-entra-id-sign-in-logs","status":"publish","type":"post","link":"https:\/\/www.stevenhodson.com\/?p=225","title":{"rendered":"PowerShell &#8211; Download Entra ID Sign-In Logs"},"content":{"rendered":"\n<p>This requires Entra ID P1 licensing to work. The first method is manual and requires you to authenticate each time you want to download the logs. You can tweak the output as needed.<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:600px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/><\/div><\/td><td><div class=\"text codecolorer\">Connect-MgGraph -TenantId &amp;lt;TenantID&gt; &nbsp;-Scopes &quot;AuditLog.Read.All&quot;<br \/>\n<br \/>\n$logs = Get-MgAuditLogSignIn -All | Select-Object CreatedDateTime, AppDisplayName, ClientAppUsed, ConditionalAccessStatus, &nbsp;@{Name=&quot;DeviceName&quot;;Expression={$_.DeviceDetail.DisplayName}}, UserDisplayName, UserPrincipalName, @{Name=&quot;City&quot;;Expression={$_.Location.City}}, @{Name=&quot;Country&quot;;Expression={$_.Location.CountryOrRegion}}, IPAddress, @{Name=&quot;ErrorCode&quot;;Expression={$_.Status.ErrorCode}}, @{Name=&quot;ErrorInfo&quot;;Expression={$_.Status.AdditionalDetails}}<br \/>\n<br \/>\n$logs | Export-Csv -Path &quot;C:\\Export\\SignInLogs.csv&quot; -NoTypeInformation<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>If you want to run this as a scheduled task and therefore won&#8217;t be able to manually authenticate each time, then you can create a self-signed certificate and an app registration in Microsoft Entra and use that instead. The following steps create a 2 year self-signed certificate that you&#8217;ll be able to use for this purpose.<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:600px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"text codecolorer\">$Certificate = New-SelfSignedCertificate -Subject MSGraphSignInLogs -CertStoreLocation Cert:\\CurrentUser\\My -NotAfter (Get-Date).AddYears(2)<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>You&#8217;ll then need to export the certificate somewhere:<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:600px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"text codecolorer\">Export-Certificate -Cert $Certificate -FilePath &quot;C:\\Export\\MSGraphSignInLogs.cer&quot;<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p>Open the certificate and copy the Certificate Thumbprint:<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"592\" height=\"760\" src=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image.png\" alt=\"\" class=\"wp-image-226\" srcset=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image.png 592w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-234x300.png 234w\" sizes=\"auto, (max-width: 592px) 100vw, 592px\" \/><\/figure>\n\n\n\n<p>Now create an App Registration in Entra ID<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"242\" src=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-1-1024x242.png\" alt=\"\" class=\"wp-image-227\" srcset=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-1-1024x242.png 1024w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-1-300x71.png 300w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-1-768x181.png 768w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-1-1536x363.png 1536w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-1-2048x484.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Give the app an appropriate name and click &#8220;Register&#8221;, leave the other settings as default.<br><br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"988\" height=\"655\" src=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-2.png\" alt=\"\" class=\"wp-image-228\" srcset=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-2.png 988w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-2-300x199.png 300w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-2-768x509.png 768w\" sizes=\"auto, (max-width: 988px) 100vw, 988px\" \/><\/figure>\n\n\n\n<p>Once the app is created, save the generated Application (client) ID and the Directory (tenant) ID somewhere:<br><br><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"436\" src=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-3-1024x436.png\" alt=\"\" class=\"wp-image-229\" srcset=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-3-1024x436.png 1024w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-3-300x128.png 300w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-3-768x327.png 768w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-3-1536x655.png 1536w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-3-2048x873.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>In the left menu pane, click in API Permissions, then click on Microsoft Graph (1) and then Application permissions:<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"501\" src=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-6-1024x501.png\" alt=\"\" class=\"wp-image-232\" srcset=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-6-1024x501.png 1024w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-6-300x147.png 300w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-6-768x376.png 768w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-6-1536x751.png 1536w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-6-2048x1001.png 2048w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><br><\/p>\n\n\n\n<p>Add the following permission &#8220;AuditLog.Read.All&#8221; and then click Update permissions<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"837\" height=\"106\" src=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-5.png\" alt=\"\" class=\"wp-image-231\" srcset=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-5.png 837w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-5-300x38.png 300w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-5-768x97.png 768w\" sizes=\"auto, (max-width: 837px) 100vw, 837px\" \/><\/figure>\n\n\n\n<p>Once you have added in the permissions, click &#8220;Grant admin consent for&#8230;&#8221; to provide the necessary access.<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"301\" height=\"30\" src=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-7.png\" alt=\"\" class=\"wp-image-233\"\/><\/figure>\n\n\n\n<p>Now move to the Certificates and secrets section in the menu pane and then select Certificates and Upload certificate:<br><\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"451\" src=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-9-1024x451.png\" alt=\"\" class=\"wp-image-235\" srcset=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-9-1024x451.png 1024w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-9-300x132.png 300w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-9-768x339.png 768w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-9-1536x677.png 1536w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-9.png 1581w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p><br>Locate the certificate file you exported earlier and then click Add<br><br><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"574\" height=\"194\" src=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-10.png\" alt=\"\" class=\"wp-image-236\" srcset=\"https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-10.png 574w, https:\/\/www.stevenhodson.com\/wp-content\/uploads\/2025\/08\/image-10-300x101.png 300w\" sizes=\"auto, (max-width: 574px) 100vw, 574px\" \/><\/figure>\n\n\n\n<p>This completes the steps in Microsoft Entra ID &#8211; now we can write the following PowerShell to complete the authentication using the certificate and app registration using the information you saved earlier and the rest of the script from above remains the same:<\/p>\n\n\n\n<pre class=\"wp-block-code\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;width:600px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/><\/div><\/td><td><div class=\"text codecolorer\">Connect-MgGraph -ClientID &amp;lt;ClientID&gt; -TenantId &amp;lt;TenantID&gt; -CertificateThumbprint &amp;lt;Thumbprint&gt;<br \/>\n<br \/>\n$logs = Get-MgAuditLogSignIn -All | Select-Object CreatedDateTime, AppDisplayName, ClientAppUsed, ConditionalAccessStatus, &nbsp;@{Name=&quot;DeviceName&quot;;Expression={$_.DeviceDetail.DisplayName}}, UserDisplayName, UserPrincipalName, @{Name=&quot;City&quot;;Expression={$_.Location.City}}, @{Name=&quot;Country&quot;;Expression={$_.Location.CountryOrRegion}}, IPAddress, @{Name=&quot;ErrorCode&quot;;Expression={$_.Status.ErrorCode}}, @{Name=&quot;ErrorInfo&quot;;Expression={$_.Status.AdditionalDetails}}<br \/>\n<br \/>\n$logs | Export-Csv -Path &quot;C:\\Export\\SignInLogs.csv&quot; -NoTypeInformation<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n\n<\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>This requires Entra ID P1 licensing to work. The first method is manual and requires you to authenticate each time you want to download the logs. You can tweak the output as needed. If you want to run this as a scheduled task and therefore won&#8217;t be able to manually authenticate each time, then you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-225","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.stevenhodson.com\/index.php?rest_route=\/wp\/v2\/posts\/225","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.stevenhodson.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.stevenhodson.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.stevenhodson.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.stevenhodson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=225"}],"version-history":[{"count":3,"href":"https:\/\/www.stevenhodson.com\/index.php?rest_route=\/wp\/v2\/posts\/225\/revisions"}],"predecessor-version":[{"id":240,"href":"https:\/\/www.stevenhodson.com\/index.php?rest_route=\/wp\/v2\/posts\/225\/revisions\/240"}],"wp:attachment":[{"href":"https:\/\/www.stevenhodson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=225"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.stevenhodson.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=225"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.stevenhodson.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=225"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}