{"id":397,"date":"2013-05-25T21:07:44","date_gmt":"2013-05-25T19:07:44","guid":{"rendered":"http:\/\/eugene.dullaard.nl\/?p=397"},"modified":"2015-04-17T13:06:59","modified_gmt":"2015-04-17T11:06:59","slug":"poweshell-password-generator","status":"publish","type":"post","link":"https:\/\/eugene.dullaard.nl\/?p=397","title":{"rendered":"Password generator function"},"content":{"rendered":"<p>Here is my take on a Microsoft Powershell password generator function. You can control which characters are used within the $chars variable if you want to change the default. I left out some characters to avoid issues with generated passwords and thus far didn&#8217;t run into problems. The code is documented by itself and you can copy and paste it into your own function script or create an independent New-Password.ps1 file.<\/p>\n<pre class=\"brush: cpp; collapse: false; title: ; notranslate\" title=\"\">\r\n#requires -version 2\r\n\r\n&lt;#\r\n.SYNOPSIS\r\nProgram  : Password Generator (New-Password)\r\nAuthor   : Eugene Dullaard\r\nDate     : 23-Dec-2012\r\n        - Initial Script\r\nUpdate   : 25-May-2013\r\n        - Added Regular Expression for checking output (complexity)\r\n        - Added default length of 8 if no length has been supplied\r\n        - Throw error if password is not meeting minimal length\r\n\r\nNew-Password generates a password out of predefined characters for a specified length.\r\n.DESCRIPTION\r\nNew-Password generates a password out of predefined characters for a specified length. The generated password is always complex, the minimal lenght is 3 characters, there is no maximum specified. If no value is given it generates an 8 character password. You can modify the script if you want to remove or add characters that can be used, currently confusing characters like lowercase L, capital O and zero are not used.\r\n.EXAMPLE\r\nNew-Password -Length 12 will give you a password with the length of 12 characters. New-Password will give you a password with a default lenght of 8 characters. You need to supply a Length if you desire a password with a different length.\r\n.EXAMPLE\r\n$Password = New-Password 8 will store an eight character password into the $password variable.\r\n#&gt;\r\n\r\nfunction New-Password\r\n  {\r\n    param (&#x5B;INT]$Length=8)\r\n\r\n    # $chars contains the variables you would like to use in your password\r\n\r\n    begin {$chars=&quot;abcdefghijkmnopqrstuvwxyz*&#x5B;];,.()&lt;&gt;!-+=_123456789ABCDEFGHJKLMNPQRSTUVWXYZ&quot;}\r\n\r\n    process\r\n      {\r\n        # Check length of password\r\n\r\n        if ($Length -le 2) {Throw &quot;Password too short&quot;}\r\n\r\n        # Start Loop to check if password matches complexity\r\n\r\n        do\r\n          {\r\n            # Make sure the variable containing the password is empty\r\n            # in case of failing to meet complexity criteria\r\n\r\n            $RandomPassword = &quot;&quot;\r\n\r\n            # Password creation process using a loop for the given length\r\n            # adding one character each turn\r\n\r\n            for ($a=1; $a -le $Length; $a++)\r\n              {$RandomPassword += $chars&#x5B;(Get-Random -Maximum $chars.Length)]}\r\n          }\r\n        until ($randompassword -match &quot;^(?=.*\\d)(?=.*&#x5B;a-z])(?=.*&#x5B;A-Z])(?=.*&#x5B;^a-zA-Z0-9])(?!.*\\s)&quot;)\r\n      }\r\n\r\n    # Return the created password\r\n\r\n    end {return $RandomPassword}\r\n  }\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is my take on a Microsoft Powershell password generator function. You can control which characters are used within the $chars variable if you want to change the default. I left out some characters to avoid issues with generated passwords and thus far didn&#8217;t run into problems. The code is documented by itself and you &hellip; <a href=\"https:\/\/eugene.dullaard.nl\/?p=397\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Password generator function<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-397","post","type-post","status-publish","format-standard","hentry","category-powershell"],"_links":{"self":[{"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=\/wp\/v2\/posts\/397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=397"}],"version-history":[{"count":10,"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=\/wp\/v2\/posts\/397\/revisions"}],"predecessor-version":[{"id":700,"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=\/wp\/v2\/posts\/397\/revisions\/700"}],"wp:attachment":[{"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/eugene.dullaard.nl\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}