Generating Passwords with Powershell

I’m generating passwords a lot these days and some customers hate complex passwords based on some random Letters, Numbers, Specials and an uppercase Letter here and there.

So why not generate a password based on a random word picked from an input file and add (or not) some fancy “complex” Special characters, numbers and/or uppercases.

This function uses four different input files to generate the passwords (two for each language). The _5 and _6 represent the length of the words (five or six letter words)

  • en_wordlist_5.txt
  • en_wordlist_6.txt
  • nl_wordlist_5.txt
  • nl_wordlist_6.txt
Function Generate-Password
    {
<# 
.Synopsis 
Script to Generated a password based on 5 and/or 6 letter word lists in Dutch or English. 
.Description 
This script will generate a password based on 2 random words from a input file (Dutch or English) completed with Uppercasing the first and/or a random letter, a random digit and/or a special character ("!","@","#","$","%","&","*","(",")","<",">","?") 
.Parameter WordLanguage 
Specify the language used for the word list (EN or NL) 
.Parameter NumLetterWords 
Specify the number of letters used for the two words (5, 6, Random) 
.Parameter UseDigits 
When used a random digit is generated between 10 and 999 
.Parameter UseSpecials 
When used a random special character is used at the end of each word ("!","@","#","$","%","&","*","(",")","<",">","?") 
.Parameter RandomUpperCase 
When used, a random letter in each word is switch to uppercase 
.Parameter FirstUpperCase 
When used, the first letter of each word is switch to uppercase 

.Example 
Generate-Password -WordLanguage EN -NumLetterWords 5 -UseDigits -RandomUpperCase -FirstUpperCase 

.Example 
Generate-Password -WordLanguage NL -NumLetterWords Random -UseDigits -RandomUpperCase -FirstUpperCase 

.Example 
Generate-Password -WordLanguage EN -NumLetterWords Random -UseDigits -UseSpecials -RandomUpperCase -FirstUpperCase 

.Notes 
Name: Generate-Password 
Author: Michael Verbeek 
Lastedit: 12/23/2015 
#>
    param(
    [ValidateSet("NL", "EN")][Parameter(Mandatory=$true, Position=0)]$WordLanguage,
    [ValidateSet("5", "6","Random")][Parameter(Mandatory=$true, Position=1)]$NumLetterWords,
    [Switch][Parameter(Mandatory=$false, Position=2)]$UseDigits,
    [Switch][Parameter(Mandatory=$false, Position=3)]$UseSpecials,     
    [Switch][Parameter(Mandatory=$false, Position=4)]$RandomUpperCase, 
    [Switch][Parameter(Mandatory=$false, Position=5)]$FirstUpperCase
    )
    
    $return = ""

    #region Validate

    if ($NumLetterWords -eq "Random") 
        { 
        $NumLetterWords1 = $(Get-Random -Minimum 5 -Maximum 7); 
        $NumLetterWords2 = $(Get-Random -Minimum 5 -Maximum 7);  
        $listtouse1 = $("$($WordLanguage)_wordlist_$($NumLetterWords1).txt").tolower()
        $listtouse2 = $("$($WordLanguage)_wordlist_$($NumLetterWords2).txt").tolower()
        if (!(Test-path $PSScriptRoot\$listtouse1)) { $text="Wordlist $listtouse1 is missing";write-host -ForegroundColor red $text; return $text; break; }
        if (!(Test-path $PSScriptRoot\$listtouse2)) { $text="Wordlist $listtouse1 is missing";write-host -ForegroundColor red $text; return $text; break; }
       
        }
    else
        {
        $listtouse1 = $("$($WordLanguage)_wordlist_$($NumLetterWords).txt").tolower()
        $listtouse2 = $listtouse1
        if (!(Test-path $PSScriptRoot\$listtouse1)) { $text="Wordlist $listtouse is missing";write-host -ForegroundColor red $text; return $text; break; }
        }

    #endregion

    $word1 = $(get-random $(Import-Csv $PSScriptRoot\$listtouse1)).Words
    $word2 = $(get-random $(Import-Csv $PSScriptRoot\$listtouse2)).words

    if($RandomUpperCase) 
        { 
        $random1 = get-random -Minimum 0 -Maximum $word1.length
        $random2 = get-random -Minimum 0 -Maximum $word2.length
        $word1 = $word1.substring(0,$random1) + $word1.substring($random1,1).toupper() + $word1.substring($random1+1,($word1.length-$random1)-1)
        $word2 = $word2.substring(0,$random2) + $word2.substring($random2,1).toupper() + $word2.substring($random2+1,($word2.length-$random2)-1)
        }

    if ($FirstUpperCase)
        {
        $word1 = $word1.substring(0,1).toupper() + $word1.substring(1)
        $word2 = $word2.substring(0,1).toupper() + $word2.substring(1)
        }

    if ($UseDigits)
        {
        $word2 = $word2 + $(Get-Random -minimum 10 -maximum 999)
        }

    if ($UseSpecials)
        {
        $word1 = $word1 + $(Get-Random -InputObject "!","@","#","$","%","&","*","(",")","<",">","?")
        $word2 = $word2 + $(Get-Random -InputObject "!","@","#","$","%","&","*","(",")","<",">","?")
        }
    
    $password = $word1 + $word2

    return $password     
    }

With the parameters -UseSpecials, -UseDigits, -RandomUppercase and -FirstUppercase you can make te output more complex if you like.

Examples:

Generate-Password -WordLanguage EN -NumLetterWords 5 -UseDigits -RandomUpperCase -FirstUpperCase

MaXimPicKy952

Generate-Password -WordLanguage NL -NumLetterWords Random -UseDigits -RandomUpperCase -FirstUpperCase

ViZorJigglE893

$result = Generate-Password -WordLanguage EN -NumLetterWords Random -UseDigits -UseSpecials -RandomUpperCase -FirstUpperCase

$result
JAuped@GLazy609<

The Generate-Password.ps1 available at the end of this blog also contains the function I use to create a complex passwords. This function is created by my colleague Arjan Mensch (https://msfreaks.wordpress.com/).

Generate-ComplexPassword -MinPasswordLength 10 -MaxPasswordLength 10

ZZGhxg4422

With these functions you can create one or more passwords to use in your own scripts.

Create 10 passwords for example:

for ($i=0;$i-lt10;$i++) { Generate-Password -WordLanguage EN -NumLetterWords 6 -UseDigits }

paxwaxfuzzed802
julepsgazabo516
quakedmuzzle391
pazazzsubfix192
quorumquokka45
frizzyquaigh285
junglequahog152
zaffirgizmos380
quaichchummy208
jerkedjibbed601

Download my functions here : Generate Passwords.zip, this zip includes the random word lists in both dutch and english for five and six letter words.

Happy scripting.

Leave a Reply

Your email address will not be published. Required fields are marked *

captcha

Please enter the CAPTCHA text

This site uses Akismet to reduce spam. Learn how your comment data is processed.