9

I am looking for something that has an usage like

passgen <length> <charset>

6 Answers 6

6

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

5
9

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:

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

You can also install the password generator module

$ModuleName = "RandomPasswordGenerator"
Get-Module -Name $ModuleName -ListAvailable | Uninstall-Module
Install-Module $ModuleName -Force -Confirm:$false
4
  • I found another batch solution, that is even close to be readable by sane humans: stackoverflow.com/questions/30629694/… Jun 24, 2018 at 13:13
  • @GrasDouble with the other batch file you can tune what type of characters you want to go into the password. Your linked question is about alphanumeric characters only. Of course it can be made to produce non-alphanumeric letters but it's not as flexible as the other question
    – phuclv
    Jun 24, 2018 at 14:06
  • +1 This answer works with PowerShell. It does not work with PowerShell Core. Aug 7, 2020 at 15:22
  • 1
    @MarkIannucci that's because PowerShell Core doesn't support System.Web.dll. Luckily there are many well-written solutions for it
    – phuclv
    Apr 13, 2021 at 6:24
1

Take a look at SecurePassword Kit command line password generator (archived link). It has the follwing syntax:

gspk.exe /g [-l:password_length] [-a:charset_options] 


Just to clarify: gspk.exe is the executable of a GUI program, but it can also be used in command line by calling it with arguments.

2
  • Read this post, you must. Apr 17, 2013 at 17:20
  • I tried this program in CLI, after execution it has to be terminated using Ctrl+C to get the prompt back, and more importantly, there seems to be no way to grab the output (e.g. to a file, and I did try >output.txt 2>&1), making the program quite useless in practice… Jun 24, 2018 at 4:06
1

You can use PowerShell, it is now cross platform, you can download PowerShell 7 from GitHub. I can give you this simple code for this task. Notice: I have only tested my code on PowerShell 7, I guarantee my code will work, however I don't know if it will work on PowerShell 5.1.

The code:

$randomstring=for ($i=0;$i -lt 100;$i++) {
    $choice=$(get-random -min 0 -max 99) % 2
    switch ($choice)
    {
        0 {[char]$(Get-Random -Min 65 -Max 90)}
        1 {[char]$(Get-Random -Min 97 -Max 122)}
    }
}
$randomstring=$randomstring -join ""
$randomstring

It will generate a random string of capital and small Latin letters with the length of 100 (100 total letters).

Sample output:

GRTkSpLsUtigeJfpENKoyoFGeUSgbWDNIQKFmDeDldtsICSIdaFAfpkKikVmyLPNojQJwrwBTjSqygEUeJiaJNjVARhpHPUWDCYw

Now if you want to add symbols and numbers:

[array]$symbols=@('+','-','*','/','?','\')
$randomstring=for ($i=0;$i -lt 100;$i++) {
    $choice=$(get-random -min 0 -max 99) % 4
    switch ($choice)
    {
        0 {[char]$(Get-Random -Min 65 -Max 90)}
        1 {[char]$(Get-Random -Min 97 -Max 122)}
        2 {$symbols[$(Get-Random -Min 0 -Max $($symbols.count-1))]}
        3 {$(Get-Random -Min 0 -Max 9)}
    }
}
$randomstring=$randomstring -join ""
$randomstring

Sample output:

CoRWBjiphnfO3fXXvQaYS?JLGWISH?wRtn-P*nS-E*y8D?E53/E4?+?q7Kd-M//?2tl5t*gU*pHdqorL5/5a0wS6N3*2SvOx-ni8

Tweak as needed; If you want a shorter string, change the number 100 in $i -lt 100 to the number you want. And if you want other symbols, put the symbol enclosed in quotes into the [array]$symbols=@('+','-','*','/','?','\') line, separate the symbols by comma;

0

Ok late but it will be helpful. I hope.

TL:DR So let me share it with you https://github.com/the-j0k3r/batch-101 and see about Simple Password Generator

A long time ago I wrote a password generator, it allowed me to generate passwords with higher entropy than %random% (since %random% is not actually random at all) and of any length on Windows, it also allows to save passwords to a file with a reference for each password, so in theory you can keep that file on a external drive for easy reference and added security.

0

deepsix will generate passwords (or many passwords) in the command line.

It is cross-platform developed in C and works on every platform I have tested it on, macOS, x86 & 64-bit Windows, and Linux. Just as you describe it offers -cNumber of Chars and -iIterations and at present offers uppercase, lowercase and numeric symbols.

I am currently adding password safe symbols in a 2.0 version - but 1.0c is available now, works locally, and does not require the Internet. It is a command-line tool, no GUI but that shouldn't be an issue. I posted the source code and several precompiled platform executables. Hopefully someone else benefits from this as I wanted one long enough I eventually wrote it myself.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .