Skip to content

Instantly share code, notes, and snippets.

@cfalta
Created November 22, 2022 21:04
Show Gist options
  • Save cfalta/ec74cf011ec973b4c268056558fa495f to your computer and use it in GitHub Desktop.
Save cfalta/ec74cf011ec973b4c268056558fa495f to your computer and use it in GitHub Desktop.
Veeeeery crude pw reset script
function Export-UserToCsV
{
Get-ADUser -filter * -Properties *| select samaccountname,description | export-csv .\userlist.csv -NoTypeInformation
}
function Get-Password([int]$Length)
{
if($Length -gt 0)
{
$Alphabet = @("0","1","2","3","4","5","6","7","8","9",":",";","<","=",">","?","!","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","_","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z")
for($i=1;$i -le $Length;$i++)
{
$Password += $Alphabet | Get-Random
}
return($Password)
}
}
function Set-AllPasswords
{
param(
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[string]$CSVPath,
[Parameter(Mandatory=$true)]
[ValidateScript({-not (Test-Path $_)})]
[string]$OutFile
)
$userlist = import-csv $CSVPath
$targetuser = $userlist | ? {$_.samaccountname -ne "krbtgt"}
Set-Content -Value "user,password" -Path $OutFile -Force
if($targetuser)
{
Write-Warning ("Resetting " + $targetuser.count + " passwords. Are you sure?")
Read-Host
foreach($user in $userlist)
{
$Password = Get-Password -Length 20
$SecurePassword = ConvertTo-SecureString $Password -AsPlainText -Force
Set-ADAccountPassword -Identity $user.samaccountname -NewPassword $SecurePassword -Reset
$outstring= $user.samaccountname + ',"' + $Password + '"'
$outstring | Add-Content -path $OutFile
$outstring
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment