PowerShell: Generate (semi) Random Password

Problem

I want to generate a random passwords that meets a few rules.

Solution

rules

A password:

  • Needs to have at least one of each of these:
    1. Lower case character
    2. Upper case character
    3. Number
  • Has a certain length.
  • Needs a random amount of each of the required types in a random order.

The goals is also to easily eliminate characters and/or numbers that might be confusing when printed out (like o and 0)

I created a cmdlet that generates passwords that meet these rules and where I can easily take characters and/or numbers out of the possible character list if they pose any readability issues.

function New-RandomPassword {
	<#
	.SYNOPSIS
		The New-RandomPassword cmdlet generates a (semi) random password that meets a few rules.
	.DESCRIPTION
		The New-RandomPassword cmdlet generates a (semi) random password that meets the following rules.:
			A password:
				- Needs to have atleast one of each of these:
					1.Lower case character
					2.Upper case character
					3.Number
				- Has a certain length.
				- Needs a random amount of each of the required types in a random order.
	.PARAMETER Length
		Length of the password
	.EXAMPLE
		New-RandomPassword 8
	.EXAMPLE
		New-RandomPassword -Length 6
	.INPUTS
		System.Int32
	.OUTPUTS
		System.String
	#>
	[CmdletBinding()]
	Param(
		[Parameter(
			Mandatory = $true,
			Position = 1
		)] [System.Int32] $Length
	)
	$arrUpperCase = @('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z')
	$arrLowerCase = @('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')
	$arrNumbers = @('1','2','3','4','5','6','7','8','9','0')
	$intLength = 8
	$intAmountU = Get-Random -Minimum 1 -Maximum ($Length-2)
	$intAmountL = Get-Random -Minimum 1 -Maximum ($Length-$intAmountU-1)
	$intAmountN = $Length-$intAmountU-$intAmountL
	$arrFormat = New-Object System.Collections.ArrayList
	for($i=0;$i -lt $intAmountU;$i++){
		[void]$arrFormat.Add('U')
	}
	for($i=0;$i -lt $intAmountL;$i++){
		[void]$arrFormat.Add('L')
	}
	for($i=0;$i -lt $intAmountN;$i++){
		[void]$arrFormat.Add('N')
	}
	$strPassword = ""
	while($arrFormat.Count -gt 0){
		$chrType = $arrFormat[(Get-Random $arrFormat.Count)]
		if($chrType -eq 'U'){
			$strPassword += (Get-Random $arrUpperCase)
		} elseif($chrType -eq 'L'){
			$strPassword += (Get-Random $arrLowerCase)
		} elseif($chrType -eq 'N'){
			$strPassword += (Get-Random $arrNumbers)
		}
		$arrFormat.Remove($chrType)
	}
	Write-Output $strPassword
}

Result

When using the cmdlet I might get the folowing result:

New-RandomPassword 8

Result:
Xz5CE1Bk
Advertisements

Tags: , , , , , ,

5 responses to “PowerShell: Generate (semi) Random Password”

  1. Tomas Roll Krognes says :

    Hey!

    Yesterday a comment on my blog noticed an error when running your password generating script.

    It looks like a wordpress formatting switch ) has gotten into the example script on your page- and this ruins the execution.

    Look at line 34 in your Random_Password_Generator.ps1-script – and remove the </p all together.

    Wrong:
    $arrNumbers = @('1','2','3','4','5','6','7','8','9','0') </p

    Correct:
    $arrNumbers = @('1','2','3','4','5','6','7','8','9','0')

    Cheers!

  2. Jon says :

    Hi,
    Nice script, although I’m getting an error from line 49 ( $strPassword = "")

    Powershell error = & : The term ‘quot’ is not recognized as the name of a cmdlet.
    Has wordpress formatted the expression incorrectly?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: