Generate Random Strings Using PowerShell

Generate Random Strings Using PowerShell

  1. Generate Random Strings With Get-Random
  2. Generate Random Passwords With the GeneratePassword() Function

Usually, when managing the Active Directory, we may have a task to import bulk users and provide them with a temporary password. However, if we need to set the password uniquely for each user, the task will be tedious unless we create a script to generate a random string.

Therefore, this article will discuss how to generate random strings that we can use for passwords using PowerShell.

Generate Random Strings With Get-Random

In PowerShell, there is a native command called Get-Random. The Get-Random cmdlet gets a randomly selected character or number.

If we submit a collection of objects to Get-Random, it gets one or more randomly selected objects from the group.

When creating passwords, we can invoke the Get-Random cmdlet four times, each time with a different input parameter (special characters, numbers, uppercase, and lowercase letters), concatenate the result strings and shuffle them using another Get-Random invoke.

Example Code:

$TokenSet = @{
        U = [Char[]]'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        L = [Char[]]'abcdefghijklmnopqrstuvwxyz'
        N = [Char[]]'0123456789'
        S = [Char[]]'!"#$%&''()*+,-./:;<=>?@[\]^_`{|}~'
    }

$Upper = Get-Random -Count 5 -InputObject $TokenSet.U
$Lower = Get-Random -Count 5 -InputObject $TokenSet.L
$Number = Get-Random -Count 5 -InputObject $TokenSet.N
$Special = Get-Random -Count 5 -InputObject $TokenSet.S

$StringSet = $Upper + $Lower + $Number + $Special

(Get-Random -Count 15 -InputObject $StringSet) -join ''

Output:

9ZimEXDW*@xo?12

The output above should generate a different string every time. However, as we can observe from the previous snippet, it takes multiple uses of the cmdlet, which makes the script block longer and processing time slower in milliseconds.

The next section of the article will discuss how we can all do the above functionalities in a couple of lines of code.

Generate Random Passwords With the GeneratePassword() Function

We can consider using the GeneratePassword() from the [System.Web.Security] if we are only to create strings used for passwords. Before we can use the function, we must first load the System.Web library by using the code below:

[Reflection.Assembly]::LoadWithPartialName("System.Web")

Once loaded, we can run the GeneratePassword() function to randomly generate a password.

Example Code:

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

Output:

QQjj*LJ:e=YZ)Fo

As we can see from the snippet of code above, the GeneratePassword() function accepts two arguments:

  1. The first argument takes an integer value representing the number of total characters needed for the password.
  2. The second argument takes an integer value representing the number of special characters for the password.

However, this code doesn’t guarantee that the password generated will have at least one numerical character. To ensure that it contains at least a numerical character, run the following snippet below.

Example Code:

do {
   $pwd = [System.Web.Security.Membership]::GeneratePassword(15,2)
} until ($pwd -match '\d')

$pwd

Output:

Y4UkK4)G+lannRd
Marion Paul Kenneth Mendoza avatar Marion Paul Kenneth Mendoza avatar

Marion specializes in anything Microsoft-related and always tries to work and apply code in an IT infrastructure.

LinkedIn

Related Article - PowerShell String

  • Array of Strings in PowerShell
  • Check if a File Contains a Specific String Using PowerShell
  • Extract a PowerShell Substring From a String
  • Extract Texts Using Regex in PowerShell
  • Escape Single Quotes and Double Quotes in PowerShell