{"id":121,"date":"2012-01-21T02:27:08","date_gmt":"2012-01-21T02:27:08","guid":{"rendered":"https:\/\/pipe2text.com\/?page_id=121"},"modified":"2013-09-30T00:55:18","modified_gmt":"2013-09-30T00:55:18","slug":"using-powershell-to-find-stale-computers","status":"publish","type":"page","link":"https:\/\/pipe2text.com\/?page_id=121","title":{"rendered":"Using PowerShell to find Stale Computers in Active Directory"},"content":{"rendered":"<p>Hello All,<\/p>\n<p>Good Active Directory hygiene is important for many reasons. One easy way to keep your directory clean is by periodically removing stale computer accounts.\u00a0Many times when a Windows machine is disjoined from a domain, rebuilt with a different name etc&#8230;, removing the computer account is often overlooked or Administrators are not notified that a machine is no longer being\u00a0used. A Windows\u00a0machine will reset its computer account password every 30 days by default. A good indicator that a Windows computer is stale is when that account has not reset its password for a good length of time such as 90 or 120 days. The following script will look for all computer accounts where the password has not been set for over 90 days. You will need to have the &#8220;Active Directory Module for PowerShell&#8221; installed on the computer you are running it from as it uses the &#8220;Get-ADComputer&#8221; cmdlet.<\/p>\n<p style=\"text-align: left;\"><span style=\"text-decoration: underline;\"><strong>The Script<\/strong><\/span><\/p>\n<p style=\"text-align: left;\">Import-Module ActiveDirectory<br \/>\n$date = [DateTime]::Today.AddDays(-90)<br \/>\nGet-ADComputer -Filter\u00a0 &#8216;PasswordLastSet -le $date&#8217; -SearchBase &#8220;OU=WhereIStoreComputers,DC=pipe2,DC=Text,DC=com&#8221; -properties PasswordLastSet<\/p>\n<p style=\"text-align: left;\"><span style=\"text-decoration: underline;\"><strong>The Output<\/strong><\/span><\/p>\n<p style=\"text-align: left;\">The output will contain the following properties for each computer account:<\/p>\n<p style=\"text-align: left;\">DistinguishedName<br \/>\nDNSHostName<br \/>\nEnabled<br \/>\nName<br \/>\nObjectClass<br \/>\nObjectGUID<br \/>\nPasswordLastSet<br \/>\nSamAccountName<br \/>\nSID<\/p>\n<p>Of course you will have to modify this script to reflect your Domain and OU structure. All you need to do is point the searchbase to the OU where you would like the search to begin and it will also search all OUs underneath it. You can also increase the number of days for a computer to be considered stale.The other important thing is to always check your list after its run before removing any accounts. There may be a good reason a machine has not reset its password for a long period of time such as the account is not for a Windows machine or it is a remote user that has not been in the office for a while.<\/p>\n<p>I decided to take this a bit further. Using some additional code, I put together something that will email a list of all computer accounts where the password has not been set for over 90 days in HTML format. This script will do the following:<\/p>\n<ul>\n<li>Check for computer accounts where the computer account password has not been reset for over 90 Days.<\/li>\n<li>Will send an HTML email using an smtp server.<\/li>\n<li>The email will have a subject that contains the name of the machine sending the report and the date when the report was generated.<\/li>\n<li>A count of how many machines have not had a password reset in over 90 days.<\/li>\n<li>A list of all machines in a table that not have not had a password reset in over 90 days including the Name, Distinguished Name and Password Last Set Date and time.<\/li>\n<\/ul>\n<p><span style=\"text-decoration: underline;\"><strong>The Script<\/strong><\/span><span style=\"text-decoration: underline;\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/pipe2text.com\/wp-content\/uploads\/2012\/01\/StaleMachineEmail.txt\">StaleMachineEmail.txt<\/a><\/p>\n<p>Since some of the lines in this script above\u00a0were too long to output properly on this page I have also inserted the StaleMachineEmail link\u00a0above containing the\u00a0 code so it is easier to copy and view. Also, if you have any\u00a0feedback\u00a0on this script, ideas\u00a0to make it\u00a0more efficient or robust please leave a comment!<\/p>\n<p>Related Links:<\/p>\n<p><a title=\"Using PowerShell and a Text File to Delete Multiple Active Directory Groups\" href=\"https:\/\/pipe2text.com\/?page_id=2067\"><strong>Using PowerShell and a Text File to Delete Multiple Active Directory Groups<\/strong><\/a><\/p>\n<p><strong><a title=\"Using PowerShell to export Active Directory Group Members to a CVS File\" href=\"https:\/\/pipe2text.com\/?page_id=1218\">Using PowerShell to export Active Directory Group Members to a CVS File<\/a><\/strong><\/p>\n<p><strong><a title=\"Using PowerShell and Active Directory to Create a Server or Workstation Inventory\" href=\"https:\/\/pipe2text.com\/?page_id=899\">Using PowerShell and Active Directory to Create a Server or Workstation Inventory<\/a><\/strong><\/p>\n<p><strong><a title=\"Using PowerShell to find Stale Computers in Active Directory\" href=\"https:\/\/pipe2text.com\/?page_id=121\">Using PowerShell to find Stale Computers in Active Directory<\/a><\/strong><\/p>\n<p><strong><a title=\"Moving Stale Computers in Active Directory to an OU using PowerShell\" href=\"https:\/\/pipe2text.com\/?page_id=750\">Moving Stale Computers in Active Directory to an OU using PowerShell<\/a><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello All, Good Active Directory hygiene is important for many reasons. One easy way to keep your directory clean is by periodically removing stale computer accounts.\u00a0Many times when a Windows machine is disjoined from a domain, rebuilt with a different &hellip; <a href=\"https:\/\/pipe2text.com\/?page_id=121\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"open","template":"","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","footnotes":""},"class_list":["post-121","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/pipe2text.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=121"}],"version-history":[{"count":45,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/121\/revisions"}],"predecessor-version":[{"id":2112,"href":"https:\/\/pipe2text.com\/index.php?rest_route=\/wp\/v2\/pages\/121\/revisions\/2112"}],"wp:attachment":[{"href":"https:\/\/pipe2text.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}