-
Notifications
You must be signed in to change notification settings - Fork 6
/
New-RandomPassword.ps1
57 lines (38 loc) · 1.4 KB
/
New-RandomPassword.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Function New-RandomPassword {
param(
[ValidateRange(6,30)]
[int]$length = 12,
[switch]$uppercase,
[switch]$lowercase,
[switch]$numbers,
[switch]$special,
[string[]]$excludedchars
)
IF (!$uppercase -and !$lowercase -and !$numbers -and !$special) {write-warning "Please specify characters to use";break}
$upperID = 65..90
$lowerID = 97..122
$numberID = 48..57
$specialID = 33..47+58..64+91..96+123..126
IF ($uppercase) {$range += $upperID}
IF ($lowercase) {$range += $lowerID}
IF ($numbers) {$range += $numberID}
IF ($special) {$range += $specialID}
$allowedchar = @()
foreach ($i in $range) {
IF ([char]$i -notin $excludedchars) {$allowedchar += [char]$i}
}
$BadPass = "I don't want to be bad"
while ($BadPass) {
if ($BadPass) {Clear-Variable badpass}
if ($RandomPassword) {Clear-Variable RandomPassword}
for ($i = 1; $i –le $length; $i++) {
$randomIndex = Get-Random -Maximum $allowedchar.count
$RandomPassword += $allowedchar[$randomIndex]
}
IF ($lowercase) {IF ($RandomPassword -cnotmatch "[a-z]") {$BadPass++}}
IF ($uppercase) {IF ($RandomPassword -cnotmatch "[A-Z]") {$BadPass++}}
IF ($numbers) {IF ($RandomPassword -notmatch "[0-9]") {$BadPass++}}
IF ($special) {IF ($RandomPassword -cnotmatch '[^a-zA-Z0-9]') {$BadPass++}}
}
Return $RandomPassword
}