I Love it! Powershell really can do a lot of cool things and this handy little function comes in for the clutch when creating new user accounts or just resetting a user’s password. Today I’m going to share with you how to Generate Strong Random Passwords Using Powershell. I know there are a ton of websites out there that can provide this functionality but this method allows you to programmatically generate passwords for easier automation. This script shall be called New-RandomPassword.

Essentially what this script does is take a number and pass it as a character data type. This is accomplished within Powershell by typing [char] SomeNumber. For example if you type: [char]35 , it should return a hash sign. To get a full list of number to characters simply run line 1 in Powershell.

33..126 | foreach {Write-Host "$_ = $([char]$_)"} #Output below 33 = ! 34 = " ... 120 = x 121 = y 122 = z

Generate Strong Random Passwords Using Powershell

There are a couple of caveats that you should be aware of so I’ll note them here. Since there are no duplicate characters allowed, each character is only used one time. Also, since there only so many characters available the max limit, per the script, is currently at 79.

Function New-RandomPassword { <# .Synopsis This will generate a new password in Powershell using Special, Uppercase, Lowercase and Numbers. The max number of characters are currently set to 79. For updated help and examples refer to -Online version. .DESCRIPTION This will generate a new password in Powershell using Special, Uppercase, Lowercase and Numbers. The max number of characters are currently set to 79. For updated help and examples refer to -Online version. .NOTES Name: New-RandomPassword Author: the Sysadmin Channel Version: 1.0 DateCreated: 2019-Feb-23 DateUpdated: 2019-Feb-23 .LINK https://thesysadminchannel.com/generate-strong-random-passwords-using-powershell/ - .EXAMPLE For updated help and examples refer to -Online version. #> [CmdletBinding()] param( [ValidateRange(4,79)] [int] $Length = 16, [switch] $ExcludeSpecialCharacters ) BEGIN { $SpecialCharacters = @((33,35) + (36..38) + (42..44) + (60..64) + (91..94)) } PROCESS { try { if (-not $ExcludeSpecialCharacters) { $Password = -join ((48..57) + (65..90) + (97..122) + $SpecialCharacters | Get-Random -Count $Length | foreach {[char]$_}) } else { $Password = -join ((48..57) + (65..90) + (97..122) | Get-Random -Count $Length | foreach {[char]$_}) } } catch { throw $_.Exception.Message } } END { Write-Output "$Password" } }

Here is what the specified output would look like.



Hopefully you will have a chance to use this script in your work flow. If you want more cool content don’t forget to check out Youtube Channel for videos and such.