· tools

Generate a Password in One Line using Bash or Powershell

Quick password generation one liners.

Sometimes you need a quick way to generate a random password for a development environment. There’s a lot of ways to do this, but here are a few one liners to use if you need something quick.

Disclaimer: These methods do not generate cryptographically secure passwords and may need adjustment to meet specific password requirements.


This one-liner redirects random noise from /dev/urandom to the tr (translate) command, which then filters it (using -dc) using a character set. Then it pipes the output to the head command, which retries a specific number of characters and echos it to the shell. Modify the character set to fit your particular password commands. Here’s a couple of examples:

tr -dc 'A-Za-z0-9!"#$%&'\''()*+,-./:;<=>?@[\]^_{|}~' </dev/urandom | head -c 20; echo

tr -dc 'A-Za-z0-9!@#$%' </dev/urandom | head -c 20; echo

Windows Powershell

[Reflection.Assembly]::LoadWithPartialName("System.Web") [System.Web.Security.Membership]::GeneratePassword(15, 2)

To be fair, this is a two liner, but it’s easy enough to understand. This loads the C# System.Web library and uses it to make a password. This generally won’t work on Powershell Core (ie. v7+).

Powershell (Core or Windows)

This command takes an array of allowed characters and randomly selects them. These characters are defined using their ASCII codes.

-join ((65..90) + (97..122) + (48..57) + 33, 64, 35, 36, 37 | ForEach-Object {[char]$_}| Get-Random -Count 20)

A less picky variant.

-join ((33..122) | ForEach-Object {[char]$_})

You can define the specific characters, which is longer, but easier to understand. -join ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%".ToCharArray() | Get-Random -Count 20)

This one uses character ranges, but character ranges only work on PowerShell Core (v7+) -join ('a'..'z' + 'A'..'Z' + '0'..'9' + "!@#$%".ToCharArray() | Get-Random -Count 20)

Back to Blog