{"id":2185,"date":"2017-05-31T20:51:34","date_gmt":"2017-05-31T20:51:34","guid":{"rendered":"http:\/\/microsoftgeek.com\/?p=2185"},"modified":"2023-05-12T13:39:36","modified_gmt":"2023-05-12T18:39:36","slug":"powershell-get-last-logon-for-all-users-across-all-domain-controllers","status":"publish","type":"post","link":"https:\/\/microsoftgeek.com\/?p=2185","title":{"rendered":"PowerShell: Get Last Logon For All Users Across All Domain Controllers"},"content":{"rendered":"<p>Here is a quick PowerShell script to help you query the last logon time for all of your users across all of your domain controllers. It will also save the output to a .csv file specified in the <strong>$exportFilePath<\/strong> string.<\/p>\n<p>I was surprised not to find many examples of this across the web already. Either not many people have multiple DC&#8217;s necessitating this or they have another more &#8220;enterprise&#8221; way of achieving the same results. Or, I&#8217;m just missing something really obvious! Either way, it was a fun script to throw together, and I hope it saves you some time in your quest to clean up AD.<\/p>\n<div>\n<pre><span style=\"color: #0000ff;\"><span style=\"color: #ff6600;\">Import-Module ActiveDirectory<\/span>\n \nfunction Get-ADUsersLastLogon()\n{\n  $dcs = Get-ADDomainController -Filter {Name -like \"*\"}\n  $users = Get-ADUser -Filter *\n  $time = 0\n  $exportFilePath = \"c:\\lastLogon.csv\"\n  $columns = \"name,username,datetime\"\n\n  Out-File -filepath $exportFilePath -force -InputObject $columns\n\n  foreach($user in $users)\n  {\n    foreach($dc in $dcs)\n    { \n      $hostname = $dc.HostName\n      $currentUser = Get-ADUser $user.SamAccountName | Get-ADObject -Server $hostname -Properties lastLogon\n\n      if($currentUser.LastLogon -gt $time) \n      {\n        $time = $currentUser.LastLogon\n      }\n    }\n\n    $dt = [DateTime]::FromFileTime($time)\n    $row = $user.Name+\",\"+$user.SamAccountName+\",\"+$dt\n\n    Out-File -filepath $exportFilePath -append -noclobber -InputObject $row\n\n    $time = 0\n  }\n}\n \nGet-ADUsersLastLogon<\/span><\/pre>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Here is a quick PowerShell script to help you query the last logon time for all of your users across all of your domain controllers. It will also save the output to a .csv file specified in the $exportFilePath string. I was surprised not to find many examples of this across the web already. Either [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[42,59],"tags":[],"class_list":["post-2185","post","type-post","status-publish","format-standard","hentry","category-ad","category-powershell"],"_links":{"self":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2185","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2185"}],"version-history":[{"count":2,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2185\/revisions"}],"predecessor-version":[{"id":3364,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=\/wp\/v2\/posts\/2185\/revisions\/3364"}],"wp:attachment":[{"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2185"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2185"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/microsoftgeek.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2185"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}