-
Notifications
You must be signed in to change notification settings - Fork 0
/
PS_Password_generator.ps1
29 lines (24 loc) · 1.09 KB
/
PS_Password_generator.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
function Generate-Password {
param (
[int]$length = 16,
[string]$characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+'
)
# Create a secure random number generator
$secureRng = New-Object System.Security.Cryptography.RNGCryptoServiceProvider
$passwordChars = New-Object System.Char[] $length
$charArray = $characters.ToCharArray()
$charArrayLength = $charArray.Length
for ($i = 0; $i -lt $length; $i++) {
$randomNumber = New-Object Byte[] 1
$secureRng.GetBytes($randomNumber)
$randomIndex = [math]::Floor($charArrayLength * ($randomNumber[0] / [byte]::MaxValue))
$passwordChars[$i] = $charArray[$randomIndex]
}
return -join $passwordChars
}
# Generate a random password with the default length of 16 characters
$generatedPassword = Generate-Password
Write-Output "Generated Password: $generatedPassword"
# Generate a random password with a custom length of 24 characters
$customLengthPassword = Generate-Password -length 24
Write-Output "Custom Length Password: $customLengthPassword"