Generate strong passwords using PowerShell

Few days ago I need to generate set of passwords for Azure SQL Database. Password policy for Azure Active Directory https://docs.microsoft.com/en-us/azure/active-directory/active-directory-passwords-policy and MSSQL https://docs.microsoft.com/en-us/sql/relational-databases/security/password-policy specifies requirements for password generator. PowerShell script for strong password generation:

$specialChars = @("!", "$", "#", "%")

$guid = [System.Guid]::NewGuid().ToString("n")
$sb = New-Object System.Text.StringBuilder

for ($i = 0; $i -lt $guid.Length; $i++)
{
    $c = $guid[$i]

    if (($i % 2 -eq 0) -and [System.Char]::IsLetter($c))
    {
        $c = [System.Char]::ToUpper($c)
    }

    $random = Get-Random -Minimum 1 -Maximum 100

    if ($random % 7 -eq 0)
    {
        $c = Get-Random $specialChars
    }

    $sb.Append($c) | Out-Null
}

$sb.ToString()


Udemy course: Improve your productivity with PowerShell

Password generator is based on the System.Guid.NewGuid() method which generates 32 character length password base containing lowercase letters and numbers. In the next step are some letters transformed to uppercase or replaced by special characters. Passwords generated by script are like this:

B0F13098C34e42729d66%eD!$#41C56d
5aB4%a9cB6Bb4c319!8dB99aC631707%
485488028d5840D0AcA04$34D48a1921

If you are interested in PowerShell automation, take my Udemy course Improve your productivity with PowerShell.

Leave a Reply

Your email address will not be published. Required fields are marked *