{"id":12841,"date":"2011-08-31T00:01:00","date_gmt":"2011-08-31T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/08\/31\/use-powershell-to-find-locked-out-user-accounts\/"},"modified":"2011-08-31T00:01:00","modified_gmt":"2011-08-31T00:01:00","slug":"use-powershell-to-find-locked-out-user-accounts","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-find-locked-out-user-accounts\/","title":{"rendered":"Use PowerShell to Find Locked-Out User Accounts"},"content":{"rendered":"<p><strong>Summary<\/strong>: Use a one-line Windows PowerShell command to find and unlock user accounts.<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hey, Scripting Guy! I am trying to find users who are locked out. For example, I have a number of users who log on only occasionally. They constantly lock themselves out. I have seen some VBScripts to search for locked out user accounts, and even a Windows PowerShell script to accomplish the same thing, but I am wondering if there is an easier way to accomplish this task. Help, please!<\/p>\n<p>&mdash;CJ<\/p>\n<p>&nbsp;<\/p>\n<p><img decoding=\"async\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\" \/>Hello CJ,<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. One problem with going on vacation is that the vacation eventually ends. I keep thinking that if we could sell our house in Charlotte, North Carolina, I might like to move to Hawaii to live. Right now, though, very few houses are actually selling in Charlotte, so there is little hope of making that move. One cool thing about living in Hawaii is that it is a couple of hours later than Redmond, Washington (Redmond is -8 GMT and Hawaii is -10 GMT). (This means that the next time someone schedules a meeting for 4:00 P.M. on a Friday, it would be 2:00 P.M. for me instead of the normal 7:00 P.M. meetings I get these days.)<\/p>\n<p>CJ, I know exactly your predicament. You have users hiding in Active Directory Domain Services (AD&nbsp;DS) who are only occasional users. AD&nbsp;DS is essentially a database, and the old adage certainly applies: garbage in, garbage out. If a user cannot remember their password, the usefulness of network security diminishes rapidly. In addition, with the integration of directory services with messaging platforms, forgotten passwords can cause problems. However, when one has hundreds or thousands&mdash;or even hundreds of thousands of users&mdash;in Active Directory, finding a locked-out user can be as big of a challenge as finding the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Frogfish\">frogfish<\/a> in the picture I took during my last scuba diving trip to Kauai.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2134.HSG-8-31-11-1.jpg\"><img decoding=\"async\" style=\"border: 0px\" title=\"Photo Ed took of a frogfish\" alt=\"Photo Ed took of a frogfish\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2134.HSG-8-31-11-1.jpg\" \/><\/a><\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp; &nbsp;This is the third in a series of three posts about working with the <b>ActiveDirectory<\/b> module. In the <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/use-active-directory-cmdlets-with-powershell-to-find-users\/\">first post<\/a>, I discussed the RSAT tools and the <b>Get-ADUser<\/b> cmdlet. In the <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/install-active-directory-management-service-for-easy-powershell-access\/\">second post<\/a>, I talked about installing the Active Directory management web service. For additional Active Directory and Windows PowerShell posts, refer to <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/active+directory\/\">this collection<\/a> on the Hey, Scripting Guy! Blog.<\/p>\n<p>When using the Microsoft Active Directory cmdlets, locating locked-out users is a snap. In fact, the <b>Search-ADAccount<\/b> cmdlet even has a <i>lockedout<\/i> switch.<\/p>\n<p>The first thing to do is to import the <b>ActiveDirectory<\/b> module by using the <b>Import-Module<\/b> cmdlet. This command is shown here:<\/p>\n<p style=\"padding-left: 30px\">Import-Module activedirectory<\/p>\n<p>Once the module is imported, use the <b>Search-ADAccount<\/b> cmdlet with the <i>lockedout <\/i>parameter. This command is shown here:<\/p>\n<p style=\"padding-left: 30px\">Search-ADAccount &ndash;LockedOut<\/p>\n<p style=\"padding-left: 60px\"><b>Note<\/b> &nbsp;&nbsp;Many network administrators who spend the majority of their time working with AD&nbsp;DS import the ActiveDirectory module via their Windows PowerShell profile. In this way, they never need to worry about first importing the module. I have an <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/getting+started\/profiles\/\">entire series of posts about working with profiles<\/a> that discusses how to create a profile, and what sort of things to add to it.<\/p>\n<p>The <b>Search-ADAccount<\/b> command and the associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2045.hsg-8-31-11-02.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of Search-ADAccount and associated output\" alt=\"Image of Search-ADAccount and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2045.hsg-8-31-11-02.png\" \/><\/a><\/p>\n<p>I can unlock the locked-out user account as well, assuming I have permission. In the following figure, I attempt to unlock the user account with an account that is a normal user. And an error arises.<\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp; &nbsp;People are often worried about Windows PowerShell from a security perspective. Windows PowerShell is only an application, and a user is not able to do anything that they do not have rights or permission to accomplish. This is a case in point.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1537.hsg-8-31-11-03.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of error\" alt=\"Image of error\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1537.hsg-8-31-11-03.png\" \/><\/a><\/p>\n<p>Because the <b>myuser<\/b> account does not have administrator rights, I need to start Windows PowerShell with an account that has the ability to unlock a user account. To do this, I right-click the Windows PowerShell icon while pressing Shift. This allows me to click <b>Run as different user<\/b> in the shortcut menu. This produces the dialog box shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7875.hsg-8-31-11-04.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of Run as different user dialog box\" alt=\"Image of Run as different user dialog box\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7875.hsg-8-31-11-04.png\" \/><\/a><\/p>\n<p>After I start Windows PowerShell again with an account that has rights to unlock users, I need to import the <b>ActiveDirectory<\/b> module once again. I then check to ensure that I can still locate the locked-out user accounts. After I have proven to myself I can do that, I pipe the results of the <b>Search-ADAccount<\/b> cmdlet to <b>Unlock-ADAccount<\/b>. A quick check ensures I have unlocked all the locked-out accounts. The series of commands is shown here:<\/p>\n<p style=\"padding-left: 30px\">import-module ActiveDirectory<\/p>\n<p style=\"padding-left: 30px\">Search-ADAccount &ndash;LockedOut<\/p>\n<p style=\"padding-left: 30px\">Search-ADAccount -LockedOut | Unlock-ADAccount<\/p>\n<p style=\"padding-left: 30px\">Search-ADAccount &ndash;LockedOut<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p>The commands and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7357.hsg-8-31-11-05.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of commands and associated output\" alt=\"Image of commands and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7357.hsg-8-31-11-05.png\" \/><\/a><\/p>\n<p style=\"padding-left: 30px\"><b>Note<\/b>&nbsp; &nbsp;Keep in mind that the command <b>Search-ADAccount -LockedOut | Unlock-ADAccount<\/b> will unlock every account that you have permission to unlock. In most cases, you will want to investigate before unlocking all locked-out accounts. If you do not want to unlock all locked-out accounts, use the <i>confirm<\/i> switch to be prompted before unlocking an account.<\/p>\n<p>If I do not want to unlock all users, I user the <i>confirm <\/i>parameter from the <b>Unlock-ADAccount<\/b> cmdlet. As an example, I first check to see which users are locked out by using the <b>Search-ADAccount<\/b> cmdlet, but I do not want to see everything, only their names. Next, I pipe the locked-out users to the <b>Unlock-ADAccount<\/b> cmdlet with the <i>confirm <\/i>parameter. I am then prompted for each of the three locked-out users. I choose to unlock the first and third users, but not the second user. I then use the <b>Search-ADAccount<\/b> cmdlet one last time to ensure that the second user is still locked out. The following figure illustrates this technique.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7026.hsg-8-31-11-06.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of technique illustrated\" alt=\"Image of technique illustrated\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/7026.hsg-8-31-11-06.png\" \/><\/a><\/p>\n<p>CJ, that is all there is to finding and unlocking users in Active Directory by using the Microsoft <b>ActiveDirectory<\/b> module. I invite you back tomorrow when I will make a historic announcement. It is good, so check back. You will be glad you did.<\/p>\n<p>&nbsp;<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Use a one-line Windows PowerShell command to find and unlock user accounts. &nbsp; Hey, Scripting Guy! I am trying to find users who are locked out. For example, I have a number of users who log on only occasionally. They constantly lock themselves out. I have seen some VBScripts to search for locked out [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[7,3,20,45],"class_list":["post-12841","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-user-accounts","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Use a one-line Windows PowerShell command to find and unlock user accounts. &nbsp; Hey, Scripting Guy! I am trying to find users who are locked out. For example, I have a number of users who log on only occasionally. They constantly lock themselves out. I have seen some VBScripts to search for locked out [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12841","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12841"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12841\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=12841"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12841"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12841"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}