New Random Password

One of the most common housekeeping tasks is to reset a password. This involves creating unique password string. So either we “imagine” a lot, or just rely on a tool that can do the work for us. Within the AD Housekeeping tasks, I very often need a new random password (Create users, password reset, service accounts, etc.)

We are starting with this random password generator because is called very often within my other scripts and modules.

Characters Sets

First of all, we are defining our 5 arrays of characters. Each of those arrays contains different characters, and are going to be used depending on the complexity.

Building Complexity Levels

At this point we have the 5 arrays with different characters, we will organize them in 4 complexity levels:

LOW: Using lowercase characters and SPACE ($LowercaseChar + $SpaceChar)

MEDIUM: Using Lowercase, Uppercase and SPACE ($LowercaseChar + $UpercaseChar + $SpaceChar)

HIGH: Using Lowercase, Uppercase, numbers and SPACE ($LowercaseChar + $UpercaseChar + $numericalChar + $SpaceChar)

VERY HIGH: Using Lowercase, Uppercase, numbers, special characters and SPACE ($LowercaseChar + $UpercaseChar + $numericalChar + $specialChar + $SpaceChar)

It is time to build the character set that we are going to use. We are going to use a switch statement and the complexity value

Now that we have the character set (with the corresponding characters based on complexity) we can continue to generate the new password.

Generating the password

We initiate by checking if the given password length is within accepted range. This range is minimum 8 characters and maximum 128.

If within the range we will “iterate” as many times as the requested length. For example, if requested password is 10, we will iterate 10 times. So each iteration will generate a new character based on the character set from last section.

Sometimes, the password MUST begin with an alphanumeric characters, otherwise we get an error. We will ensure that the first character is a letter.

Finally, we will ensure that no duplicated characters are generated for our new password.

Last, but not least, is to “drop” the new random password to the “pipeline”. Many people like to use “Write-Host”, but this is mainly for screen and not fully recommended. Instead, use a combination of verbose messages with the “Write-Output” for the password.

Full Powershell Script as a function

Social network sharing