PowerShell: Random Password Generator

There are many really good tools capable of generating random passwords with different complexity and purposes. Some of them are integrated in password managers, some embedded in the browser others available programmatically from the cli with no need to use an online password generator.

I guess there are several thousands functions or blog posts on the internet around this topic, but I think that I can add some interesting information that will be useful to everybody at any level of experience, so let’s start!


My absolute favourite is pwgen that creates a lot of possible passwords meeting my needs , e.g with these options secure, unambiguous and containing symbols with the exact length of 10 characters.

Or 3 password 50 character long and so on:

Please check the man page for pwgen for all the possible options.

This tool is so simple and efficient. I use it in every OS, with Windows and WSL is straightforward to use it (and simple to get it with any package manager)within the cmd prompt or from powershell adding just WSL before pwgen command to have the same result. With MacOS and home-brew is just as simple to install it.

When do I need to generate password?

This question is interesting. A password should expire (frequently) and should be unique (as definition of secret and security). For this reason creating a new password and having the ability to generate it must simple and fast as possible.
There are many password manager tools available and most of them will provide or suggest a randomly generated password for you. Also Google Chrome can generate a password with a right-click on the password field, mozilla firefox has many add-ins or plugins with a similar feature.

In almost every company the most popular use for generating random password is more likely a part of an on-boarding script for generate AD Users or an automated password reset, when is required to generate a random password and not to depend from third-party modules if possible or desirable.

I prefer to re-use code as especially aiming for a robust solution and focus my efforts on solving problems and write code that is not already part of a library/api or module, but to create a lightweight password generator are just few lines of code.

My PowerShell simplified version of pwgen

This powershell script generates a password.  It’s a simple function that has an integer parameter for the password length (between 8 and 32 characters), generates a random choice from any available common ascii code and validate the result with a Regular Expression , just to check if the password is strong enough to meet the common password complexity requirements of Active Directory.

And if I want a result like formatted like pwgen I just need 2 nested for loops:

This is the end result, very similar indeed.

As usual, you can find this on my github repository.


Adding several unit tests for an automated script is key for a robust application, in this case the script is simple, but the results is really interesting:

Code coverage of 100% and hundreds of tests passed make me feel confident that this function is resilient and in case a new issue will be found, I can simply add a specific test to catch/prevent it from happening again in case feature changes are modifying the behaviour of my code.

And this is the Pester script used for unit testing:


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.