PowerShell for Windows Admins

Jun 26 2017   10:10AM GMT

Generating passwords

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

Tags:
"Computer security"
Password
Powershell

Generating new passwords can be a painful business.  There are many ways of accomplishing  password generation –  depending on your needs.  One suggestion for generating passwords is to use a GUID as the basis of the password.

PS> New-Guid

Guid
----
269f328d-b80d-446a-a14c-6197ff1bcc40

You could then remove the hyphens and extract part of the guid

PS> (New-Guid).Tostring() -replace '-'
c5023096aee24b3ba1d9988ff1c774e4

You need to decide the length of the password.  Guids are 32 characters so make sure you start your extraction in a position that gives room for the length you need

$length = 8

((New-Guid).Tostring() -replace '-').Substring((Get-Random -Minimum 0 -Maximum (31-$length)), $length)

Your results will look like these examples

fbe8e66e
 980d4032
 0341d71f
 6f6478fd
 fbfea1ce
 34694bc6
 62666733
 b1419ac0
 3cf8aa7d

The drawback is that you only have numbers and lower case characters

If you use the Membership class from System.Web you can do this

PS> Add-Type -AssemblyName System.Web
PS> [System.Web.Security.Membership]::GeneratePassword(8,2)
1L*q381)

The GeneratePassword method takes 2 arguments – the first is the length of the password and the second is the number of non-alphanumeric characters

If you are running PowerShell v5 you can utilise the using keyword instead of Add-Type

PS> using assembly System.Web
PS> [System.Web.Security.Membership]::GeneratePassword(8,1)
f>jg84XR

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: