Passwords, Passwords, and more Passwords. Let’s generate a 16 character password that is complex and random that you will have to save into your password manager because you will never remember it.

The Single Line

[string]::Join("",(1..16 | ForEach-Object {[char](Get-Random -Minimum 32 -Maximum 126)}))        

The Breakdown

Lets break it down and then make a function for easier use. We are going to use the concept of PEMDAS. For this breakdown.

Get-Random -Minimum 32 -Maximum 126

This gives us a random number between 32 and 126. Why is this important. The next part is why. We are grabbing a character of X, [char](x), These are considered password-safe characters of the ASCII set.

 1..16 | foreach-object {SomeCode}

This part repeats everything in the “some code” area 16 times. So we are grabbing 16 chars. Each loop occurs separately. This creates 16 characters that take up a different line on the shell prompt each time it runs. That’s where the next part comes into play.

[string]::Join("", array )

This part of the script is a string function that joins each part of the array together. Notice the “” part. This adds items inside the array. So if you want the password to have 7 every join, then place “7” here.

Now when you combine all this together. We create an array of random password-safe characters and join them all together. With their powers combined, we have a potential password.

Lets make a Function

function Get-SHDPassword {
    [cmdletbinding()]
    Param (
        [int]$Length = 16,
        [int]$Count = 1
    )
    for ($I=0;$I -lt $Count;$I++) {
        [string]::Join("",(1..$Length | ForEach-Object {[char](Get-Random -Minimum 32 -Maximum 126)}))        
    }
}

Here we added a count, so we can make more than one and choose from it. By default, we are setting them to 16 and to 1. This way we have a 16 character password that is done only once.

That’s all folks, let me know if you have any questions or corrections.