PowerShell to Generate a Random Password

Are you looking for a quick way to create strong and secure passwords for multiple accounts or web applications? PowerShell is an easy-to-use terminal language that lets you generate random passwords quickly, helping save time and keeping your data safe. In this blog post, we’ll explore how to use PowerShell commands to generate random passwords with various length and complexity requirements. So let’s get started!

How to generate a random string in PowerShell?

To generate a random string in PowerShell, you can use the following script:

-join ((97..122) | Get-Random -Count 10 | ForEach-Object {[char]$_})

This will generate a random string of length 10, containing lowercase letters. E.g.

lyknbwdrxu

nlqgyuptac

iemrvdakzx

phxcvwdknb

You can adjust the length of the string by changing the value of the -Count parameter. For example, to generate a string of length 20, you would use -Count 20.

You can also modify the characters that are included in the string by adjusting the range of ASCII values passed to Get-Random. For example, to generate a string containing uppercase letters, lowercase letters, and digits, you can use the following command:

$RandomString = -join ((48..57) + (65..90) + (97..122) | Get-Random -Count 10 | ForEach-Object {[char]$_})

This will generate a random string of length 10, containing uppercase letters and digits. To create a random string from given characters, use:

-Join("ABCDabcd&@#$%1234".tochararray() | Get-Random -Count 10 | % {[char]$_})

You can use this technique to generate random passwords of any length, containing any combination of characters. Let’s take a look at how to generate a random password in PowerShell by wrapping the script into a reusable function.

Random Password Generator using PowerShell

A powerful password should include uppercase letters, lowercase letters, special characters, and numbers. It’s not always easy to come up with these passwords on the spot, so you may want to consider generating a random password. Here’s how to create a random password in PowerShell.

Function Get-RandomPassword
{
    #define parameters
    param([int]$PasswordLength = 10)

    #ASCII Character set for Password
    $CharacterSet = @{
            Uppercase   = (97..122) | Get-Random -Count 10 | % {[char]$_}
            Lowercase   = (65..90)  | Get-Random -Count 10 | % {[char]$_}
            Numeric     = (48..57)  | Get-Random -Count 10 | % {[char]$_}
            SpecialChar = (33..47)+(58..64)+(91..96)+(123..126) | Get-Random -Count 10 | % {[char]$_}
    }

    #Frame Random Password from given character set
    $StringSet = $CharacterSet.Uppercase + $CharacterSet.Lowercase + $CharacterSet.Numeric + $CharacterSet.SpecialChar

    -join(Get-Random -Count $PasswordLength -InputObject $StringSet)
}

#Call the function to generate random password of 8 characters
Get-RandomPassword -PasswordLength 8

#Sample Output: glx`FC>Y

This function will generate a completely randomized secure password that meets all modern security standards and requirements (a combination of upper, and lower case alphabets, numbers, and special characters). Call the Get-RandomPassword function to generate a 10-character long random password.

generate random password powershell

Here is the alternative way to create a secure random password using .NET framework method:

[System.Web.Security.Membership]::GeneratePassword(10,2)

This command generates a random 10-character password that contains two non-alphanumeric characters (e.g., %&). If you want more or fewer characters for your password, simply change the number 10 in the command to whatever length of password you desire (e.g., 8).

PowerShell Random password one-liner: -join([char[]](33..122) | Get-Random -Count 10)

Conclusion:

Creating strong passwords is an essential part of staying safe online in today’s world. Fortunately, there’s no need to spend hours trying to think of complex passwords when you can easily generate them using PowerShell commands! With just a few simple steps, you can quickly create totally randomized passwords that meet current security standards – and keep prying eyes away from your data!

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

Leave a Reply

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