Recently I was needing to create a bunch of random passwords for users and I needed to have it work in Powershell.
Since Lee Holmes’ comments, I’ve updated the file and function with some of the suggestions he gave. Unfortunately, the <# #> help syntax throws a bunch of errors in V1, so I had to revert to my old ways.
@'
<#
.Synopsis
Create a random string of letters.
.Description
Create a random string of letters (upper and lower) and numbers for
use in passwords.
.Parameter Length
How long do you want the password to be? (Default is 8.)
.Example
PS> createPassword 10
.ReturnValue
[string]
.Link
.Notes
NAME: Verb-Noun
AUTHOR: Josh Erickson
LASTEDIT: 1/21/2009
#Requires -Version 2.0
#>
'@ | out-null
function createPassword {
param(
[int] $length = 8
);
if($length -eq $Null) {
$length = 8;
}
$tmppwd = @();
$rand = new-object system.random -argumentlist $(date).tick;
for($i = 0; $i -lt $length; $i++) {
#caps 65, 90
$a = $rand.next(0,100);
if($a -lt 33) {
$tmppwd += ,[char]$rand.next(97,122);
} elseif($a -ge 34 -and $a -lt 66) {
$tmppwd += ,[char]$rand.next(65,90);
} else {
$tmppwd += ,[char]$rand.next(48,57);
}
}
[string]::join("",$tmppwd);
}

