function Generate-ComplexPassword {
param(
[Parameter(Mandatory=$true)]
[int]$Length
)
# Define character sets
$lowercase = "abcdefghijklmnopqrstuvwxyz"
$uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
$numbers = "0123456789"
$specialChars = "!@#$%^&*()_-+=[]{}|;:,.<>/?"
$allChars = $lowercase + $uppercase + $numbers + $specialChars
# Ensure password length is at least 15 characters
if ($Length -lt 15) {
Write-Error "Password length must be at least 15 characters."
return
}
# Generate initial password with at least one character from each set
$password = @(
Get-Random -InputObject $lowercase.ToCharArray()
Get-Random -InputObject $uppercase.ToCharArray()
Get-Random -InputObject $numbers.ToCharArray()
Get-Random -InputObject $specialChars.ToCharArray()
)
# Fill the rest of the password to the desired length
for ($i = $password.Count; $i -lt $Length; $i++) {
$password += Get-Random -InputObject $allChars.ToCharArray()
}
# Shuffle the password to avoid predictable patterns
$password = $password | Get-Random -Count $password.Count
# Output the password as a single string
-join $password
}
# Example usage:
# Generate-ComplexPassword -Length 15
Above code allows you to generate complex passwords from within PowerShell. Load the function into powershell and start generating passwords:
