/* ]]> */
Sep 122011
 

Here we have a simple function to generate a complex password in Windows Powershell.

To Add this to YOUR script, include:

  1. The system.web assembly (add this somewhere in your script
    (where it will only get called once)
  2. The GenPwd function

That’s all there is to it. Passwords will be 8 characters long;  change the $pwdLen variable if you want a different length.

The system.web.generatepassword call will guarantee that at least one “special” non-alphanumeric character is used but does not guarantee anthing more.

To ensure other complexities are present, we test the new password against a regular expression that tests for:

  • at least one uppercase character
  • at least one lowercase character
  • at least one numeric character

If the generated password doesn’t match the regular expression,  we keep retrying until it does.

In this sample script, the function  “GenPwd” is called  in two ways:

  1. TestMode shows how many tries it takes to generate a password
  2. Real Mode simply returns a password from the function call

Download Link: Generate-Password.ps1 (right-click and select “save as…”)

Sample output:

 Posted by at 4:06 pm

  One Response to “Generate Complex Passwords with PowerShell”

  1. I use just a one liner like this:
    1..10 | ForEach { $Password = $Password + [char]((Get-Random -Minimum 33 -Maximum 127) + (Get-Random -Maximum 2)*32) }

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">

Page optimized by WP Minify WordPress Plugin