forked from BornToBeRoot/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGet-RandomPassword.ps1
186 lines (158 loc) · 3.85 KB
/
Get-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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
###############################################################################################################
# Language : PowerShell 4.0
# Filename : Get-RandomPassword.ps1
# Autor : BornToBeRoot (https://github.com/BornToBeRoot)
# Description : Generate passwords with a freely definable number of characters
# Repository : https://github.com/BornToBeRoot/PowerShell
###############################################################################################################
<#
.SYNOPSIS
Generate passwords with a freely definable number of characters
.DESCRIPTION
Generate passwords with a freely definable number of characters. You can also select which chars you want to use (upper case, lower case, numbers and special chars).
.EXAMPLE
Get-RandomPassword -DisableSpecialChars
Password
--------
Rzxy48Nu
.EXAMPLE
Get-RandomPassword -Length 6 -Count 10
Count Password
----- --------
1 xxy&x9
2 $sX9nr
3 0(@60w
4 aCNlaP
5 eY$MLi
6 R?V0U6
7 -C9mSu
8 -Beat*
9 h_DaVc
10 u+H}%]
.LINK
https://github.com/BornToBeRoot/PowerShell/blob/master/Documentation/Function/Get-RandomPassword.README.md
#>
function Get-RandomPassword
{
[CmdletBinding(DefaultParameterSetName='NoClipboard')]
param(
[Parameter(
Position=0,
HelpMessage='Length of the Password (Default=8)')]
[ValidateScript({
if($_ -eq 0)
{
throw "Length of the password can not be 0!"
}
else
{
return $true
}
})]
[Int32]$Length=8,
[Parameter(
ParameterSetName='NoClipboard',
Position=1,
HelpMessage='Number of Passwords to be generated (Default=1)')]
[ValidateScript({
if($_ -eq 0)
{
throw "Number of Passwords to be generated can not be 0"
}
else
{
return $true
}
})]
[Int32]$Count=1,
[Parameter(
ParameterSetName='Clipboard',
Position=1,
HelpMessage='Copy password to clipboard')]
[switch]$CopyToClipboard,
[Parameter(
Position=2,
HelpMessage='Use lower case characters (Default=$true')]
[switch]$DisableLowerCase,
[Parameter(
Position=3,
HelpMessage='Use upper case characters (Default=$true)')]
[switch]$DisableUpperCase,
[Parameter(
Position=4,
HelpMessage='Use upper case characters (Default=$true)')]
[switch]$DisableNumbers,
[Parameter(
Position=5,
HelpMessage='Use upper case characters (Default=$true)')]
[ValidateScript({
if($DisableLowerCase -and $DisableUpperCase -and $DisableNumbers -and $_)
{
throw "Select at least 1 character set (lower case, upper case, numbers or special chars) to create a password."
}
else
{
return $true
}
})]
[switch]$DisableSpecialChars
)
Begin{
}
Process{
$Character_LowerCase = "abcdefghiklmnprstuvwxyz"
$Character_UpperCase = "ABCDEFGHKLMNPRSTUVWXYZ"
$Character_Numbers = "0123456789"
$Character_SpecialChars = "$%&/()=?+*#[]{}-_@"
$Characters = [String]::Empty
# Built string with characters
if($DisableLowerCase -eq $false)
{
$Characters += $Character_LowerCase
}
if($DisableUpperCase -eq $false)
{
$Characters += $Character_UpperCase
}
if($DisableNumbers -eq $false)
{
$Characters += $Character_Numbers
}
if($DisableSpecialChars -eq $false)
{
$Characters += $Character_SpecialChars
}
for($i = 1; $i -ne $Count + 1; $i++)
{
$Password = [String]::Empty
# Create random password
while($Password.Length -lt $Length)
{
# Create random numbers
$RandomNumber = Get-Random -Maximum $Characters.Length
$Password += $Characters[$RandomNumber]
}
# Return result
if($Count -eq 1)
{
# Set to clipboard
if($CopyToClipboard)
{
Set-Clipboard -Value $Password
}
[pscustomobject] @{
Password = $Password
}
}
else
{
[pscustomobject] @{
Count = $i
Password = $Password
}
}
}
}
End{
}
}