PowerShell Quick Tip 01 – Password Generator

Posted: 29/07/2010 in PowerShell

I should start by pointing out that I’ll not be doing a PowerShell basics post as this has been done to death by the blogging community. If you don’t have much experience with PowerShell I suggest you google for this information or better still take a look at the excellent CBTnuggets PowerShell on-the-job training over at http://www.cbtnuggets.com/series?id=456 .

One of the things I love about PowerShell is the way that you can effortlessly interact with .NET and this example will do just that by hooking into the System.Web Assembly (in System.Web.dll). Further details about this class can be found at http://msdn.microsoft.com/en-us/library/system.web.security.membership.generatepassword.aspx

First you’ll need to load the System.Web assembly with this line:

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

Now you can create passwords with the following line:

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

NOTE: The two numeric values in parentheses represent password length and the amount of non-alphanumeric characters used respectively.

You can use this to create a batch of 50 passwords:

1..50| % {[System.Web.Security.Membership]::GeneratePassword(10,2)}

Or maybe just one very large password!:

[String]::Join("",(1..50| % {[System.Web.Security.Membership]::GeneratePassword(100,2)}))

Enjoy,

jfrmilner

Advertisements

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