{"id":2258,"date":"2014-01-09T00:01:00","date_gmt":"2014-01-09T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/01\/09\/use-powershell-to-get-account-lockout-and-password-policy\/"},"modified":"2014-01-09T00:01:00","modified_gmt":"2014-01-09T00:01:00","slug":"use-powershell-to-get-account-lockout-and-password-policy","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-get-account-lockout-and-password-policy\/","title":{"rendered":"Use PowerShell to Get Account Lockout and Password Policy"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft guest blogger and PFE, Ian Farr, talks about using Windows PowerShell to get account lockout and password policies.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Welcome back guest blogger, Ian Farr. Ian is a Microsoft PFE in the UK.<\/p>\n<p>Recently, I was asked how to retrieve a domain&rsquo;s <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/cc757692(v=WS.10).aspx#w2k3tr_sepol_accou_set_tdtx\" target=\"_blank\">Account Lockout Policy<\/a> and <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/cc757692(v=WS.10).aspx#w2k3tr_sepol_accou_set_kuwh\" target=\"_blank\">Password Policy<\/a> with Windows PowerShell.<b> <\/b>Both are stored as attributes on each domain&rsquo;s <a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/bb123839(v=EXCHG.65).aspx\" target=\"_blank\">Domain Naming Context<\/a>. In fact, when you update these policies with the Group Policy Management Console, it is the role of the domain&rsquo;s <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/cc223752.aspx\" target=\"_blank\">PDC emulator<\/a> to write the changes to the header of the domain naming context.<\/p>\n<p>This function reads policy information from a domain header: <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Get-ADDomainAccountPolicies-d3a97a4f\" target=\"_blank\">Get-ADDomainAccountPolicies<\/a>.<\/p>\n<h2><b>Account Lockout Policy<\/b><\/h2>\n<p>Here&rsquo;s how to get the Account Lockout Policy settings. First, connect to the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/windows\/desktop\/ms684291(v=vs.85).aspx\" target=\"_blank\">RootDSE<\/a> of a domain controller:<\/p>\n<p style=\"margin-left:30px\">$RootDSE = Get-ADRootDSE -Server $Domain<\/p>\n<p>Use <b>Get-ADObject<\/b> to retrieve properties from the domain naming context (<b>defaultNamingContext<\/b>):<\/p>\n<p style=\"margin-left:30px\">$AccountPolicy = Get-ADObject $RootDSE.defaultNamingContext -Property lockoutDuration, lockoutObservationWindow, lockoutThreshold&nbsp;<\/p>\n<p>Next, produce a customized output representing the policy.<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp; $AccountPolicy | Select @{n=&quot;PolicyType&quot;;e={&quot;Account Lockout&quot;}},`<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DistinguishedName,`<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @{n=&quot;lockoutDuration&quot;;e={&quot;$($_.lockoutDuration \/ -600000000) minutes&quot;}},`<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @{n=&quot;lockoutObservationWindow&quot;;e={&quot;$($_.lockoutObservationWindow \/ -600000000) minutes&quot;}},`<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lockoutThreshold | Format-List<\/p>\n<p>&nbsp;<b>Select-Object<\/b> displays some standard properties: <b>DistinguishedName<\/b> and <b>LockoutThreshold<\/b>. It also displays some custom properties constructed with the aid of two hash tables. Let&rsquo;s look at the format of the lockout duration value:&nbsp;<\/p>\n<p style=\"margin-left:30px\">@{n=&quot;lockoutDuration&quot;;e={&quot;$($_.lockoutDuration \/ -600000000) minutes&quot;}}<\/p>\n<ul>\n<li><b>@{}<\/b> denotes the hash table, which contains one or more key-value pairs<\/li>\n<li><b>n<\/b> is the name of the property to be stored, and its value is <b>lockoutDuration<\/b><\/li>\n<li><b>e<\/b> is the expression that Windows PowerShell evaluates, and that value is&hellip;<\/li>\n<\/ul>\n<p>What&rsquo;s the expression doing? The <b>LockoutDuration<\/b> property of the current object &nbsp;<b>$_<\/b> is a negative 64-bit time value interval expressed in nanoseconds. This is divided by -600000000 to give a positive value in minutes. This calculation is performed as part of a subexpression, which is recognized by this notation: <b>&lsquo;$( )&rsquo;<\/b>. The code in brackets is calculated first. The returned value is then added to a string where <b>minutes<\/b> is appended and stored as the value associated with <b>e<\/b>.<\/p>\n<p>We now have a&nbsp;hash table with two elements, <b>n<\/b> and <b>e<\/b>, that <b>Select-Object<\/b> recognizes and formats into a display property to the console screen. The previous process also applies to the <b>LockoutObservationWindow<\/b> value.<\/p>\n<p>Here&rsquo;s some sample output:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-9-14-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-9-14-1.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2><b>Password Policy<\/b><\/h2>\n<p>Again, use <b>Get-ADObject<\/b> to retrieve properties from the domain naming context:<\/p>\n<p style=\"margin-left:30px\">$PasswordPolicy = Get-ADObject $RootDSE.defaultNamingContext -Property minPwdAge, maxPwdAge, minPwdLength, pwdHistoryLength, pwdProperties&nbsp;<\/p>\n<p>Next, produce a customized output that represents the policy:<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp; $PasswordPolicy | Select @{n=&quot;PolicyType&quot;;e={&quot;Password&quot;}},`<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; DistinguishedName,`<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @{n=&quot;minPwdAge&quot;;e={&quot;$($_.minPwdAge \/ -864000000000) days&quot;}},`<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @{n=&quot;maxPwdAge&quot;;e={&quot;$($_.maxPwdAge \/ -864000000000) days&quot;}},`<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; minPwdLength,`<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pwdHistoryLength,`<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; @{n=&quot;pwdProperties&quot;;e={Switch ($_.pwdProperties) {<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 0 {&quot;Passwords can be simple and the administrator account cannot be locked out&quot;}<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 1 {&quot;Passwords must be complex and the administrator account cannot be locked out&quot;}<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 8 {&quot;Passwords can be simple, and the administrator account can be locked out&quot;}<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 9 {&quot;Passwords must be complex, and the administrator account can be locked out&quot;}<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Default {$_.pwdProperties}}}}<\/p>\n<p>We&rsquo;re doing similar stuff here with the hash tables, except that we calculate days, not minutes, from the negative nanosecond value.<\/p>\n<p>For the last value returned, we&rsquo;re taking <b>$_.pwdProperties<\/b> and attempting to match it to a known value by using a <b>Switch<\/b> statement. For example, if <b>$_.pwdPropeties<\/b> is 8, then the value associated with <b>e<\/b> will be &quot;Passwords can be simple, and the administrator account can be locked out.&quot;<\/p>\n<p>Here is a sample output:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-9-14-2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-9-14-2.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Not the prettiest function, but certainly effective!<\/p>\n<p>You can download the entire function from the Script Center Repository: <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Get-ADDomainAccountPolicies-d3a97a4f\" target=\"_blank\">Get-ADDomainAccountPolicies Function<\/a>.<\/p>\n<p>~Ian<\/p>\n<p>Thank you, Ian, for once again sharing your time and knowledge.<\/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\" target=\"_blank\">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","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft guest blogger and PFE, Ian Farr, talks about using Windows PowerShell to get account lockout and password policies. Microsoft Scripting Guy, Ed Wilson, is here. Welcome back guest blogger, Ian Farr. Ian is a Microsoft PFE in the UK. Recently, I was asked how to retrieve a domain&rsquo;s Account Lockout Policy and Password [&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,56,472,3,198,45],"class_list":["post-2258","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-active-directory","tag-guest-blogger","tag-ian-farr","tag-scripting-guy","tag-users","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft guest blogger and PFE, Ian Farr, talks about using Windows PowerShell to get account lockout and password policies. Microsoft Scripting Guy, Ed Wilson, is here. Welcome back guest blogger, Ian Farr. Ian is a Microsoft PFE in the UK. Recently, I was asked how to retrieve a domain&rsquo;s Account Lockout Policy and Password [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2258","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=2258"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2258\/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=2258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}