9

I am looking for something that has an usage like

passgen <length> <charset>
phuclv
  • 30,396
  • 15
  • 136
  • 260
John
  • 1,215

6 Answers6

11

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 = &quot;&quot;
    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
phuclv
  • 30,396
  • 15
  • 136
  • 260
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, check the other answers.

Unix/Linux

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

PythoNic
  • 155
slhck
  • 235,242
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;

Ξένη Γήινος
  • 3,902
  • 13
  • 45
  • 84
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.

Gras Double
  • 1,140
  • 1
  • 14
  • 21
Tapkin
  • 11
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.

phuclv
  • 30,396
  • 15
  • 136
  • 260
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.