{"id":163,"date":"2017-08-07T20:56:15","date_gmt":"2017-08-07T20:56:15","guid":{"rendered":"http:\/\/www.edrockwell.com\/blog\/?p=163"},"modified":"2017-08-07T20:56:15","modified_gmt":"2017-08-07T20:56:15","slug":"powershell-how-to-find-all-users-where-account-is-inactive","status":"publish","type":"post","link":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/","title":{"rendered":"PowerShell How To: Find all users where account is inactive"},"content":{"rendered":"<p>I was recently asked asked to find all the users in Active Directory where their account was inactive. <\/p>\n<p>There is a PowerShell commandlet called Search-ADAccount that you can use to find if the account is inactive by using the parameter -AccountInactive.<\/p>\n<p>This is kind of crude but works well. I couldn&#8217;t figure out how to get the headers into the csv so I simply did a write-output for the first section.<\/p>\n<pre class=\"lang:ps decode:true \" title=\"Find Inactive AD usres\" >\r\n#######################\r\n# Ed Rockwell\r\n# Free to use\r\n# Version 1.0\r\n# 8\/7\/2017\r\n#######################\r\n$time = 90 # Days since last login\r\n$users = Search-ADAccount -AccountInactive -UsersOnly -TimeSpan $time # Get all users within that timeframe with AccountInactive Property greater than $time\r\n$path = \"C:\\Powershell\\AccountInactive\" # Where to write file\r\n\r\n#File Name\r\nnew-item $path\\users.csv -Force\r\n\r\n# Set the header of csv (Change this if you add to the write-output below)\r\nwrite-output \"$(\"SamAccountName\"),$(\"Enabled\"),$(\"PasswordExpired\"),$(\"LastLogonDate\"),$(\"OU Location\")\"  | add-content -path $path\"\\users.csv\"\r\n\r\n# Find users \r\nforeach ($user in $users) \r\n    {\r\n        If ($user.DistinguishedName -notmatch 'OU=Disabled Users' -and $user.DistinguishedName -notmatch 'OU=Service Accounts' -and $user.DistinguishedName -notmatch 'CN=Microsoft Exchange System Objects')\r\n            {\r\n                $DN = $user.distinguishedname -split ',' \r\n                $container = $DN[1]\r\n                write-output \"$($user.SamAccountName),$($user.Enabled),$($user.PasswordExpired),$($user.LastLogonDate),$($container)\" | add-content -path $path\"\\users.csv\"\r\n            }\r\n    }<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I was recently asked asked to find all the users in Active Directory where their account was inactive. There is a PowerShell commandlet called Search-ADAccount that you can use to find if the account is inactive by using the parameter -AccountInactive. This is kind of crude but works well. I couldn&#8217;t figure out how to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[68,16],"tags":[69,70,21],"class_list":["post-163","post","type-post","status-publish","format-standard","hentry","category-active-directory","category-powershell","tag-active-directory","tag-find-inactive-accounts","tag-powershell-2"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PowerShell How To: Find all users where account is inactive - System Admin [RESOLVED]<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PowerShell How To: Find all users where account is inactive - System Admin [RESOLVED]\" \/>\n<meta property=\"og:description\" content=\"I was recently asked asked to find all the users in Active Directory where their account was inactive. There is a PowerShell commandlet called Search-ADAccount that you can use to find if the account is inactive by using the parameter -AccountInactive. This is kind of crude but works well. I couldn&#8217;t figure out how to [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/\" \/>\n<meta property=\"og:site_name\" content=\"System Admin [RESOLVED]\" \/>\n<meta property=\"article:published_time\" content=\"2017-08-07T20:56:15+00:00\" \/>\n<meta name=\"author\" content=\"Ed Rockwell\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@EddieRock\" \/>\n<meta name=\"twitter:site\" content=\"@EddieRock\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ed Rockwell\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/\"},\"author\":{\"name\":\"Ed Rockwell\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7\"},\"headline\":\"PowerShell How To: Find all users where account is inactive\",\"datePublished\":\"2017-08-07T20:56:15+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/\"},\"wordCount\":82,\"commentCount\":0,\"keywords\":[\"Active Directory\",\"Find Inactive Accounts\",\"Powershell\"],\"articleSection\":[\"Active Directory\",\"PowerShell\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/\",\"url\":\"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/\",\"name\":\"PowerShell How To: Find all users where account is inactive - System Admin [RESOLVED]\",\"isPartOf\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#website\"},\"datePublished\":\"2017-08-07T20:56:15+00:00\",\"author\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.edrockwell.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PowerShell How To: Find all users where account is inactive\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#website\",\"url\":\"https:\/\/www.edrockwell.com\/blog\/\",\"name\":\"System Admin [RESOLVED]\",\"description\":\"How To: Make My DevOps Life Easier\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.edrockwell.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7\",\"name\":\"Ed Rockwell\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g\",\"caption\":\"Ed Rockwell\"},\"sameAs\":[\"http:\/\/www.therockwells.net\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"PowerShell How To: Find all users where account is inactive - System Admin [RESOLVED]","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/","og_locale":"en_US","og_type":"article","og_title":"PowerShell How To: Find all users where account is inactive - System Admin [RESOLVED]","og_description":"I was recently asked asked to find all the users in Active Directory where their account was inactive. There is a PowerShell commandlet called Search-ADAccount that you can use to find if the account is inactive by using the parameter -AccountInactive. This is kind of crude but works well. I couldn&#8217;t figure out how to [&hellip;]","og_url":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/","og_site_name":"System Admin [RESOLVED]","article_published_time":"2017-08-07T20:56:15+00:00","author":"Ed Rockwell","twitter_card":"summary_large_image","twitter_creator":"@EddieRock","twitter_site":"@EddieRock","twitter_misc":{"Written by":"Ed Rockwell","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/#article","isPartOf":{"@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/"},"author":{"name":"Ed Rockwell","@id":"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7"},"headline":"PowerShell How To: Find all users where account is inactive","datePublished":"2017-08-07T20:56:15+00:00","mainEntityOfPage":{"@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/"},"wordCount":82,"commentCount":0,"keywords":["Active Directory","Find Inactive Accounts","Powershell"],"articleSection":["Active Directory","PowerShell"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/","url":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/","name":"PowerShell How To: Find all users where account is inactive - System Admin [RESOLVED]","isPartOf":{"@id":"https:\/\/www.edrockwell.com\/blog\/#website"},"datePublished":"2017-08-07T20:56:15+00:00","author":{"@id":"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7"},"breadcrumb":{"@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.edrockwell.com\/blog\/powershell-how-to-find-all-users-where-account-is-inactive\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.edrockwell.com\/blog\/"},{"@type":"ListItem","position":2,"name":"PowerShell How To: Find all users where account is inactive"}]},{"@type":"WebSite","@id":"https:\/\/www.edrockwell.com\/blog\/#website","url":"https:\/\/www.edrockwell.com\/blog\/","name":"System Admin [RESOLVED]","description":"How To: Make My DevOps Life Easier","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.edrockwell.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.edrockwell.com\/blog\/#\/schema\/person\/e181b98aceccea720abee0c25bbfbcd7","name":"Ed Rockwell","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dea3152d7acd78aa28d280207c33f7ec516731ece9a54249cbbbebcfffc341e0?s=96&d=mm&r=g","caption":"Ed Rockwell"},"sameAs":["http:\/\/www.therockwells.net"]}]}},"_links":{"self":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts\/163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/comments?post=163"}],"version-history":[{"count":3,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts\/163\/revisions"}],"predecessor-version":[{"id":166,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/posts\/163\/revisions\/166"}],"wp:attachment":[{"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/media?parent=163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/categories?post=163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.edrockwell.com\/blog\/wp-json\/wp\/v2\/tags?post=163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}