{"id":72412,"date":"2015-07-02T00:01:00","date_gmt":"2015-07-02T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/07\/02\/using-powershell-to-check-lockout-threshold-for-domains\/"},"modified":"2019-02-18T09:47:14","modified_gmt":"2019-02-18T16:47:14","slug":"using-powershell-to-check-lockout-threshold-for-domains","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/using-powershell-to-check-lockout-threshold-for-domains\/","title":{"rendered":"Using PowerShell to Check Lockout Threshold for Domains"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Ed Wilson, Microsoft Scripting Guy, talks about using Windows PowerShell to check the lockout threshold for several domains.<\/span>\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\">&nbsp;Hey, Scripting Guy! I have several domains in our forest, and it seems that some weasel got in and changed the lockout threshold in some of the child domains. I know we should have turned on auditing, but we did not. What I need right now is a way to find all of the domains that have been changed.\n&mdash;MB\n<img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\">&nbsp;Hello MB,\nMicrosoft Scripting Guy, Ed Wilson, is here. This weekend, there is a battle-of-the-bands going on in downtown Charlotte. I am thinking that the Scripting Wife and I may head down there. If we are lucky, they may play &ldquo;Heard it Through the Pipeline&rdquo; or &ldquo;Highway to PowerShell.&rdquo;&nbsp;\nMB, luckily, it is fairly easy to accomplish what you want to do. Here is an example you can use that would work for any property you want to check.<\/p>\n<h2>Get the domains in the forest<\/h2>\n<p>The first thing to do is to get a list of all of the domains in the forest. To do this, I can use the <b>Get-ADForest<\/b> cmdlet, and select only the domains. After I do that, I walk through the domains, get the default domain password policy for each domain, and compare it to a reference policy. When I have the comparison, I create a custom object for each domain policy that does not match. Here is how I go about it:<\/p>\n<p style=\"margin-left:30px\">Import-Module activedirectory<\/p>\n<p style=\"margin-left:30px\">$default = Get-ADDefaultDomainPasswordPolicy -Identity nwtraders.com<\/p>\n<p style=\"margin-left:30px\">Foreach ($domain in (Get-ADForest).domains)\nI like to import the Active Directory module directly because it is a bit faster than doing a lookup and finding the module. Besides, I know that I am going to be using the Active Directory module, so it certainly does not hurt to import it.\nNow I read the default domain password policy that I know is correct. I store this in a variable I call <b>$Default<\/b>, and then I get my collection of domains.<\/p>\n<h2>Check each domain<\/h2>\n<p>Now I need to get the default password policy for each domain in my collection of domains. I then use the <b>Compare-Object<\/b> cmdlet to compare the default policy with what I retrieved from the current domain. I am only comparing the <b>LockOutThreshold<\/b>. Here is the code that does this:<\/p>\n<p style=\"margin-left:30px\">$p = Get-ADDefaultDomainPasswordPolicy -Identity $domain<\/p>\n<p style=\"margin-left:30px\">&nbsp;$diff = Compare-Object -ReferenceObject $default -DifferenceObject `<\/p>\n<p style=\"margin-left:30px\">&nbsp; $p -Property lockoutthreshold -PassThru\nBecause I use the <b>&ndash;Passthru<\/b> parameter, the difference objects return to the <b>$diff<\/b> variable I specified. I now want to walk through each of the objects stored in the <b>$diff<\/b> variable and look for a side indicator that is <b>&lsquo;&lt;=&rsquo;<\/b>. This will mean that there is a difference between the reference object and the object I am comparing. When I find this difference, I grab the domain name and the lockout threshold and create a custom object. This code is shown here:<\/p>\n<p style=\"margin-left:30px\">Foreach ($d in $diff)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp; {if($d.sideindicator -eq &#8216;&lt;=&#8217;)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {[pscustomobject]@{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;DomainName&#8217; = $d.distinguishedname ;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;LockOutThreshold&#8217; = $d.LockoutThreshold}}}}\nThe complete script appears here:<\/p>\n<p style=\"margin-left:30px\">Import-Module activedirectory<\/p>\n<p style=\"margin-left:30px\">$default = Get-ADDefaultDomainPasswordPolicy -Identity nwtraders.com<\/p>\n<p style=\"margin-left:30px\">Foreach ($domain in (Get-ADForest).domains)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;$p = Get-ADDefaultDomainPasswordPolicy -Identity $domain<\/p>\n<p style=\"margin-left:30px\">&nbsp;$diff = Compare-Object -ReferenceObject $default -DifferenceObject `<\/p>\n<p style=\"margin-left:30px\">&nbsp; $p -Property lockoutthreshold -PassThru<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp; Foreach ($d in $diff)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp; {if($d.sideindicator -eq &#8216;&lt;=&#8217;)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {[pscustomobject]@{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;DomainName&#8217; = $d.distinguishedname ;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#8216;LockOutThreshold&#8217; = $d.LockoutThreshold}}}}\nMB, that is all there is to using Windows PowerShell to check Active Directory domain password policies. Active Directory Week will continue tomorrow when I will talk about more cool stuff.\nI invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"http:\/\/blogs.technet.commailto: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.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about using Windows PowerShell to check the lockout threshold for several domains. &nbsp;Hey, Scripting Guy! I have several domains in our forest, and it seems that some weasel got in and changed the lockout threshold in some of the child domains. I know we should have turned on [&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,45],"class_list":["post-72412","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Ed Wilson, Microsoft Scripting Guy, talks about using Windows PowerShell to check the lockout threshold for several domains. &nbsp;Hey, Scripting Guy! I have several domains in our forest, and it seems that some weasel got in and changed the lockout threshold in some of the child domains. I know we should have turned on [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72412","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=72412"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/72412\/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=72412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=72412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=72412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}