1

Powershell Random Password Generator

I Love it! Powershell really can do a lot of cool things and this handy little function comes in for the clutch when creating new user accounts or just resetting a user’s password. Today I’m going to share with you a simple, yet effective Powershell Random Password Generator. I know there are a ton of websites out there that can provide this functionality but this method allows you to programmatically generate passwords for easier automation.

A great use case for this random password generator script would be to generate a strong random password for an account creation. This way you have everything setup in your script without having to manually ping a website elsewhere. This script shall be called New-RandomPassword.

How The Random Password Generator Powershell Script Works

Essentially what this script does is take a number and pass it as a character data type. This is accomplished within Powershell by typing [char] SomeNumber. For example if you type: [char]35 , it should return a hash sign. To get a full list of number to characters simply run line 1 in Powershell.

33..126 | foreach {Write-Host "$_ = $([char]$_)"}
#Output below
33 = !
34 = "
...
120 = x
121 = y
122 = z

Powershell Random Password Generator

There are a couple of caveats that you should be aware of so I’ll note them here. Since there are no duplicate characters allowed, each character is only used one time. Also, since there only so many characters available the max limit, per the script, is currently at 79.

Script Requirements

  • There are no requirements but you should be using Powershell 5 or greater

Parameters

    -Length

Description: This will specify the amount of characters you want to output. Valid ranges are 5 to 79 and the default is currently set to 16.

    -ExcludeSpecialCharacters

Description: Special Characters include: !#$%&*+,<=>?@[\]^ . When this switch is called, these characters will not be in the output

Examples

Example 1: New-RandomPassword
Example 2: New-RandomPassword -Length 20
Example 3: New-RandomPassword -Length 20 -ExcludeSpecialCharacters

Function New-RandomPassword { 
<#
.Synopsis
    This will generate a new password in Powershell using Special, Uppercase, Lowercase and Numbers.  The max number of characters are currently set to 79.
    For updated help and examples refer to -Online version.


.NOTES   
    Name: New-RandomPassword
    Author: theSysadminChannel
    Version: 1.0
    DateCreated: 2019-Feb-23


.LINK 
    https://thesysadminchannel.com/generate-strong-random-passwords-using-powershell/ -


.EXAMPLE
    For updated help and examples refer to -Online version.

#>

    [CmdletBinding()]
    param(
        [Parameter(
            Position = 0,
            Mandatory = $false
        )]
        [ValidateRange(5,79)]
        [int]    $Length = 16,

        [switch] $ExcludeSpecialCharacters

    )


    BEGIN {
        $SpecialCharacters = @((33,35) + (36..38) + (42..44) + (60..64) + (91..94))
    }

    PROCESS {
        try {
            if (-not $ExcludeSpecialCharacters) {
                    $Password = -join ((48..57) + (65..90) + (97..122) + $SpecialCharacters | Get-Random -Count $Length | foreach {[char]$_})
                } else {
                    $Password = -join ((48..57) + (65..90) + (97..122) | Get-Random -Count $Length | foreach {[char]$_})
            }

        } catch {
            Write-Error $_.Exception.Message
        }

    }

    END {
        Write-Output $Password
    }

}

 

Here is what the specified output would look like.
Powershell Random Password Generator

Hopefully you will have a chance to use this script in your work flow or automation. Personally, I use this function on a weekly basis for whenever I need to generate strong passwords longer than 20 characters. Anytime I need to sign up for a new service, New-RandomPassword -Length 20 is all I need to do for the password portion. I love it.

If you like these kinds of Powershell Posts, be sure to check our Powershell repo full of real world scripts like this one that you can make use of. While you’re at, don’t forget to check out our YouTube Channel for sysadmin video content I’m sure you’ll love too.

4.6/5 - (22 votes)

Paul Contreras

Hi, my name is Paul and I am a Sysadmin who enjoys working on various technologies from Microsoft, VMWare, Cisco and many others. Join me as I document my trials and tribulations of the daily grind of System Administration.

One Comment

Leave a Reply

Your email address will not be published.