{"id":35,"date":"2012-05-15T13:15:32","date_gmt":"2012-05-15T13:15:32","guid":{"rendered":"https:\/\/sysadminnightmare.com\/?p=35"},"modified":"2012-05-15T13:15:32","modified_gmt":"2012-05-15T13:15:32","slug":"finding-when-a-user-account-last-logged-in","status":"publish","type":"post","link":"https:\/\/sysadminnightmare.com\/index.php\/2012\/05\/15\/finding-when-a-user-account-last-logged-in\/","title":{"rendered":"Finding when a user account last logged in"},"content":{"rendered":"<p>I had a simple request the other day that I wanted to log on this site since I\u00a0vaguely remember having to do it before.<\/p>\n<p>A quick background &#8211;\u00a0Our company has merged with another company which means we will be busy in the coming months with implementing a new domain. One thing we need to do is create new accounts on the new domain for all the users in each of the existing domains. Since we have thousands of user accounts\u00a0 (some internal and some external or contractors) we needed a way to find out which of these accounts have been used in an X number of days.<\/p>\n<p>The easiest and quickest way I could think to do this is to create a PowerShell\u00a0Script to query the AD and get the Last Login variable from the account. Problem that I found was that the AD account will record the Login with the Domain Controller that it logs in to. If you have more than one domain controller (you have more than one right!!??) the domain controllers don&#8217;t\u00a0sync that login info. So below is the code used to query the domain controllers and get the last logon info from each.<\/p>\n<p>Disclaimer &#8211; I didn&#8217;t not write this code but found it after sorting through several google searches. If you wrote this .. thank you. I am putting it here for my own reference so next time I am asked to get the information I will know right where to look.<\/p>\n<p>[code]<\/p>\n<p>$datetime\u00a0= Get-Date -Format \u201cyyyyMMddhhmmss\u201d;<\/p>\n<p># Create and Add Headers to CSV<\/p>\n<p>Add-Content \u201cc:tempLastLogin &#8211; $datetime.csv\u201d \u201cUser,Last Logon\u201d;<\/p>\n<p>Get-QADUser\u00a0| ForEach-Object {<br \/>\nWrite-Host $_.Name<br \/>\nWrite-Host $_.LastLogon<br \/>\n$user = $_.Name<br \/>\n$time = $_.LastLogon<br \/>\n$sc = [char]34<\/p>\n<p># Add Data to CSV<\/p>\n<p>Add-Content \u201cc:tempLastLogin &#8211; $datetime.csv\u201d \u201c$sc$user$sc,$sc$time$sc\u201d<\/p>\n<p>}<\/p>\n<p>[\/code]<\/p>\n<p>Steps are to:<\/p>\n<ol>\n<li>Create a file with the above code and save as LastLogin.ps1<\/li>\n<li>Run the file using\u00a0PowerShell (.LastLogin.PS1)<\/li>\n<li>Open and read &#8220;LastLogin &#8211; DateTime.csv&#8221;<\/li>\n<\/ol>\n<p>I also combined this with a PowerShell\u00a0Script to create a SharePoint List but that is a different post on a different Blog (at a different time)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I had a simple request the other day that I wanted to log on this site since I\u00a0vaguely remember having to do it before. A quick background &#8211;\u00a0Our company has merged with another company which means we will be busy in the coming months with implementing a new domain. One&#8230; <a class=\"continue-reading-link\" href=\"https:\/\/sysadminnightmare.com\/index.php\/2012\/05\/15\/finding-when-a-user-account-last-logged-in\/\"> Continue reading <span class=\"meta-nav\">&rarr; <\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2],"tags":[12,35],"class_list":["post-35","post","type-post","status-publish","format-standard","hentry","category-admin","tag-ad-users","tag-powershell"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2bgeE-z","_links":{"self":[{"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/posts\/35","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/comments?post=35"}],"version-history":[{"count":0,"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"wp:attachment":[{"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sysadminnightmare.com\/index.php\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}