What command line tool can generate passwords on Windows?

I am looking for something that has an usage like

passgen <length> <charset>

Solution 1:

Cross-Platform: Using PERL

There's a PERL password generator that works for Windows, *nix, OS X, etc.

Usage: genpass [-OPTIONS] LENGTH
-s, --symbols       Exclude symbol characters.
-n, --numbers       Exclude number characters.
-u, --uppercase     Exclude uppercase characters.
-l, --lowercase     Exclude lowercase characters.

On Windows, it can be converted into an executable.

Windows-only

Warning: This will change the password for the administrator account.

Not specifically what you want, but could also come in useful. In the command line, type:

net user administrator /random    

enter image description here

Unix/Linux

See answers of this question: Random password generator: many, in columns, on command line, in Linux

Solution 2:

Depends on how strong you want your password to be. Of course if security is not a concern then echo %random%%random% in cmd or Get-Random -Minimum 100000000 in PowerShell will just work

With PowerShell you have another better option: call GeneratePassword(int length, int numberOfNonAlphanumericCharacters). For example to generated a 12-character long password with at least 3 special symbols you can call it like this

# Just need to call once at the start of the script
[Reflection.Assembly]::LoadWithPartialName("System.Web")

[System.Web.Security.Membership]::GeneratePassword(12, 3)

There are several MS TechNet blog posts on this:

  • Generating a New Password with Windows PowerShell
  • How to Generate a Secure Random Password using PowerShell
  • PowerShell Random Password Generator

In case you don't want to work with PowerShell then here's a pure batch solution


.NET Core does not support System.Web.dll so it doesn't have [System.Web.Security.Membership]::GeneratePassword() and you'll have to write a custom a password generator or find some available code on the internet. Here's one in Password generation in PowerShell Core (6+)

$symbols = '!@#$%^&*'.ToCharArray()
$characterList = 'a'..'z' + 'A'..'Z' + '0'..'9' + $symbols
function GeneratePassword {
    param(
        [Parameter(Mandatory = $false)]
        [ValidateRange(12, 256)]
        [int] 
        $length = 14
    )
    
    do {
        $password = ""
        for ($i = 0; $i -lt $length; $i++) {
            $randomIndex = [System.Security.Cryptography.RandomNumberGenerator]::GetInt32(0, $characterList.Length)
            $password += $characterList[$randomIndex]
        }

        [int]$hasLowerChar = $password -cmatch '[a-z]'
        [int]$hasUpperChar = $password -cmatch '[A-Z]'
        [int]$hasDigit = $password -match '[0-9]'
        [int]$hasSymbol = $password.IndexOfAny($symbols) -ne -1

    }
    until (($hasLowerChar + $hasUpperChar + $hasDigit + $hasSymbol) -ge 3)
    
    $password | ConvertTo-SecureString -AsPlainText
}

See also

  • https://gist.github.com/Jaykul/5cb0410abd40672707faf67549404ea8
  • https://mohitgoyal.co/2017/01/13/generate-random-password-using-powershell/

You can also install the password generator module

$ModuleName = "RandomPasswordGenerator"
Get-Module -Name $ModuleName -ListAvailable | Uninstall-Module
Install-Module $ModuleName -Force -Confirm:$false