Generating passwords

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’re 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
Advertisements
This entry was posted in Powershell, Security. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s