Generate Random Password in PowerShell


In this tutorial, you’ll learn how to quickly and easily generate strong and secure passwords for multiple accounts or in the web using random password generator in Windows PowerShell.

Generate Random Password in PowerShell

PowerShell provides a way to quickly generate random passwords to save time and protect your data. In this post, you will learn how to generate random passwords with different length and complexity requirements using PowerShell commands.

Supporting a random password generator in PowerShell has several benefits. It ensures that the generated passwords are truly random, making them more secure from brute-force attacks than passwords based on predictable patterns or personal information. Additionally, a random password generator allows for automation, making it ideal for scenarios where you need to quickly generate multiple passwords, such as creating user accounts in active directory or setting up service accounts. By using PowerShell, you can create complex and unique passwords without the need for additional tools.

Based on the command known from *nix systems, I call it mkpasswd.

function mkpasswd {
    param (
        [int] $length,
        [int] $NumberOfSpecialChars = 1
    Add-Type -AssemblyName 'System.Web'
    return [System.Web.Security.Membership]::GeneratePassword($length, $NumberOfSpecialChars)

Save the lines in the file Microsoft.PowerShell_profile.ps1

The file Microsoft.PowerShell_profile.ps1 must be in the user’s Documents folder, under the WindowsPowerShell folder.


This mkpasswd function takes two parameters. The length parameter determines the length of the password, while the NumberOfSpecialChars parameter specifies the number of special characters in the password. By changing these parameters, you can generate passwords that meet your needs.

How to generate a secure password in PowerShell

Open PowerShell and run the password generator with entermkpasswdand hit Enter

PS C:\> mkpasswd 12

The number indicates the lenght of the password should have. Without specifying the password length, the prompt asks you to enter a number.

quote Hit. use this one-liner to quick generate a secure password.

PS C:\> -join([char[]](33..122) | Get-Random -Count 12)


Generating strong passwords is an essential part of connected security in today’s global world. Luckily, you don’t have to spend hours struggling with complex passwords when you can easily generate them using PowerShell commands. With just a few actions you can quickly create random passwords that meet current security standards. This way we keep prying eyes away from our data.

How useful was this post?

Click on a star to rate it!

Average rating / 5. Vote count:

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Leave a Reply

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