Thursday, 31 January 2019

Powershell Script to Create Random Complex Passwords suitable for O365



This script enables you to generate a set of random complex passwords for a list of users which is imported as a CSV file.  The password will contain upper and lowercase letters, symbols, numbers and will always be between 8-16 characters in length (meeting the O365 requirements).  Obviously the functions can be tweaked to make the passords more or less secure and the script could be altered to add more sections to the password.  Remember each time a function is called within the foreach, the variable name will need to be different in order to stop the same part of the password repeating.  That’s why there are multiple random symbol and random numbers.




$JobStart = Get-Date

$userlistpath = "C:\cloudwyse\users_requiring_passwords.csv"

$wordlistpath = "C:\cloudwyse\dictionary.csv"

$DateTime = (Get-Date -Format "ddMMyyyy-HHmmss")

$exportpath = "C:\Cloudwyse\users_with_passwords$DateTime.csv"

Write-Host  -ForegroundColor Magenta "Importing lists from $userlistpath and $wordlistpath..."

$userlist = import-csv $userlistpath

$wordlist = import-csv $wordlistpath

$JobEnd = Get-Date

$JobSecondsTaken = ($JobEnd - $JobStart)

Write-Host -ForegroundColor Yellow "Lists imported taking" $JobSecondsTaken.Minutes "minute(s) and" $JobSecondsTaken.Seconds "second(s)."

$symbollist = @("`^","`!","`%","`&")

function RandomWord {$wordlist[(get-random -maximum 716)] | select-object -ExpandProperty Word}

function RandomNumber {get-random -minimum 10 -maximum 99}

function RandomSymbol {$symbollist[(get-random -maximum 4)]}

$total = $null

$Job2Start = Get-Date

$pwList = @()

foreach ($user in $userlist)     {

       $pwPart1 = RandomSymbol

       $pwPart2a = RandomWord

       $pwPart2 = (Get-Culture).TextInfo.ToTitleCase($pwPart2a.ToLower())

       $pwPart3 = RandomSymbol

       $pwPart4 = RandomNumber

       $pwPart5 = RandomNumber

       $longpass = "$pwPart1$pwPart2$pwPart3$pwPart4"

       if ($longpass.length -lt 8) {   

              do {

             $longpass += $pwPart5


             until ($longpass.length -ge 8)


       $password = $longpass.substring(0, [System.Math]::Min(16, $longpass.Length))

       $username = $user | Select-Object -ExpandProperty User

       $pw = New-Object PSObject

       $pw | Add-Member -type NoteProperty -Name 'User' -Value $username

       $pw | Add-Member -type NoteProperty -Name 'Password' -Value $password

       Write-Host  -ForegroundColor Magenta "Processed password for user" $username

       $pwlist += $pw

       $total = $total +1


$Job2End = Get-Date

$Job2SecondsTaken = ($Job2End - $Job2Start)

Write-Host -ForegroundColor Yellow "Processed $total passwords in" $Job2SecondsTaken.Minutes "minute(s) and" $Job2SecondsTaken.Seconds "second(s)."

Write-Host  -ForegroundColor Magenta "Exporting list..."

$pwList | Export-csv -Path $exportpath

Write-Host -ForegroundColor Yellow "Finished exporting list to $exportpath"



The CSV file I used for the dictionary can be downloaded here.




No comments:

Post a comment