-3

I am looking for a PowerShell function that generates a random password. I have found many however not a single one does what I want.

I want function that will generate a password X inputted length while using Y special characters, either securely or plain text.

Any recommendations ?

dcaz
  • 847
  • 6
  • 15
  • See also: [PowerShell - Password Generator - How to always include number in string?](https://stackoverflow.com/a/37275209/1701026) – iRon Jul 14 '21 at 06:48

3 Answers3

2
Function New-Password {
    [cmdletbinding()]
    Param(
        $Passwordlength = 10,

        $Specialcharacters = 2
    )

    begin {
        Add-Type -AssemblyName System.Web
    }

    process {
        Write-Verbose "Creating new password $passwordlength characters long with $specialcharacters special characters"
        [System.Web.Security.Membership]::GeneratePassword($passwordlength,$specialcharacters)
    }
    
}
Doug Maurer
  • 8,090
  • 3
  • 12
  • 13
  • 1
    Thanks Doug! This works like I want. I think I can tweak it to give it a secure string option. – dcaz Jul 14 '21 at 01:53
0

For generating the passwords, you can use the secret module in python. It provides access to the most secure source of randomness that your operating system provides.

For generating an eight-character alphanumeric password:

import string
import secrets
password_len = 8
alphabet = string.ascii_letters + string.digits
password = ''.join(secrets.choice(alphabet) for i in range(password_len))

Here is the official documentation of the secret module.

Happy Coding :)

Mohit kumar
  • 155
  • 1
  • 5
0
function New-Password {
    <#
    .SYNOPSIS
        Generate a random password.
    .DESCRIPTION
        Generate a random password.
    .NOTES
        Change log:
            27/11/2017 - faustonascimento - Swapped Get-Random for System.Random.
                                            Swapped Sort-Object for Fisher-Yates shuffle.
            17/03/2017 - Chris Dent - Created.
    #>

    [CmdletBinding()]
    [OutputType([String])]
    param (
        # The length of the password which should be created.
        [Parameter(ValueFromPipeline)]        
        [ValidateRange(8, 255)]
        [Int32]$Length = 12,

        # The character sets the password may contain. A password will contain at least one of each of the characters.
        [String[]]$CharacterSet = ('abcdefghijklmnopqrstuvwxyz',
                                   'ABCDEFGHIJKLMNPQRSTUVWXYZ',
                                   '123456789',
                                   '!$%&^-@'),

        # The number of characters to select from each character set.
        [Int32[]]$CharacterSetCount = (@(1) * $CharacterSet.Count)
    )

    begin {
        $bytes = [Byte[]]::new(4)
        $rng = [System.Security.Cryptography.RandomNumberGenerator]::Create()
        $rng.GetBytes($bytes)

        $seed = [System.BitConverter]::ToInt32($bytes, 0)
        $rnd = [Random]::new($seed)

        if ($CharacterSet.Count -ne $CharacterSetCount.Count) {
            throw "The number of items in -CharacterSet needs to match the number of items in -CharacterSetCount"
        }

        $allCharacterSets = [String]::Concat($CharacterSet)
    }

    process {
        try {
            $requiredCharLength = 0
            foreach ($i in $CharacterSetCount) {
                $requiredCharLength += $i
            }

            if ($requiredCharLength -gt $Length) {
                throw "The sum of characters specified by CharacterSetCount is higher than the desired password length"
            }

            $password = [Char[]]::new($Length)
            $index = 0
        
            for ($i = 0; $i -lt $CharacterSet.Count; $i++) {
                for ($j = 0; $j -lt $CharacterSetCount[$i]; $j++) {
                    $password[$index++] = $CharacterSet[$i][$rnd.Next($CharacterSet[$i].Length)]
                }
            }

            for ($i = $index; $i -lt $Length; $i++) {
                $password[$index++] = $allCharacterSets[$rnd.Next($allCharacterSets.Length)]
            }

            # Fisher-Yates shuffle
            for ($i = $Length; $i -gt 0; $i--) {
                $n = $i - 1
                $m = $rnd.Next($i)
                $j = $password[$m]
                $password[$m] = $password[$n]
                $password[$n] = $j
            }

            [String]::new($password)
        } catch {
            Write-Error -ErrorRecord $_
        }
    }
}

is something I found. https://gist.github.com/indented-automation/2093bd088d59b362ec2a5b81a14ba84e

dcaz
  • 847
  • 6
  • 15