Automating Managed Password Change Using PowerShell

Wanted to share a script I wrote a couple months ago for another PFE to create random passwords, log them to a text file and change a SharePoint managed account. 

#Identify the account to change
$Account = "contoso\svcSPExcel"
$ManagedAccount = Get-SPManagedAccount $Account

#Generate the Random Password
$rand = New-Object System.Random
#Generate a new 20 character password
$pwd = ""
1..20 | ForEach { $pwd = $pwd + [char]$rand.next(33,127) }

#Set the Password as a secure string
$securePassword = ConvertTo-SecureString -String $pwd -AsPlainText -Force

#Log the Change prior to the change
$Text = "SharePoint Managed Account Password Changed for: " + $Account + " to " + $pwd
Add-Content c:\temp\PasswordChanges.log $Text
#Reset the Password
Set-SPManagedAccount -Identity $ManagedAccount –Password $securePassword -Confirm:$false
#Repeat lines 1-18 for each managed account you want to change.