Password Generator Script

This script, authored by Richard Easton, is designed to generate random passwords. It provides a flexible way to customize the length and complexity of generated passwords.

Prerequisites


The Script


####################################
# Author: Richard Easton
# Description: Random Password generator
# Usage: New-RandomPassword -MinimumPasswordLength 10 -MaximumPasswordLength 15 -NumberOfAlphaNumericCharacters 6
####################################

function New-RandomPassword {
    param(
        [Parameter()]
        [int]$MinimumPasswordLength = 8,
        [Parameter()]
        [int]$MaximumPasswordLength = 16,
        [Parameter()]
        [int]$NumberOfAlphaNumericCharacters = 5,
        [Parameter()]
        [switch]$ConvertToSecureString
    )
    
    Add-Type -AssemblyName 'System.Web'
    $length = Get-Random -Minimum $MinimumPasswordLength -Maximum $MaximumPasswordLength
    $password = [System.Web.Security.Membership]::GeneratePassword($length,$NumberOfAlphaNumericCharacters)
    if ($ConvertToSecureString.IsPresent) {
        ConvertTo-SecureString -String $password -AsPlainText -Force
    } else {
        $password
    }
}

How It Works

Parameter Options

The script takes three parameters: MinimumPasswordLength, MaximumPasswordLength, and NumberOfAlphaNumericCharacters. These options allow you to customize the generated password's length and complexity.

Password Generation Logic

The script uses the Get-Random cmdlet to generate a random number within the specified range for the password length. Then, it uses the [System.Web.Security.Membership]::GeneratePassword() method to create a password of the specified length with the desired number of alphanumeric characters.


Usage Examples

To use this script, simply call the New-RandomPassword function and specify your desired password length and complexity. For example:


New-RandomPassword -MinimumPasswordLength 10 -MaximumPasswordLength 15 -NumberOfAlphaNumericCharacters 6

Conclusion

This script provides a simple yet powerful way to generate random passwords. By customizing the length and complexity of generated passwords, you can ensure strong and unique passwords for your users.