Skip to content

Instantly share code, notes, and snippets.

@ip75
Last active November 1, 2022 17:20
Show Gist options
  • Save ip75/9b9b1e18b2946659d9756854009c3df4 to your computer and use it in GitHub Desktop.
Save ip75/9b9b1e18b2946659d9756854009c3df4 to your computer and use it in GitHub Desktop.
domain Password Policy By By. C# code to set random password several times that domain password policy require and then set your lovely password.
void Main()
{
string sDomain = "ad.microsoft.com";
string sServiceUser = "username";
string sServicePassword = "account_old_password";
string sNewServicePassword = "account_new_password";
var pc = new PrincipalContext(ContextType.Domain, sDomain, sServiceUser, sServicePassword);
UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(pc, sServiceUser);
//oUserPrincipal.Dump();
var firstPass = sServicePassword;
var newPass = sNewServicePassword;
var passwordlsList = new List<string>();
passwordlsList.Add(firstPass);
foreach(var i in Enumerable.Range(0, 20))
{
passwordlsList.Add($"BSp@ssw0rd{i}");
}
passwordlsList.Add(newPass);
for (var i = 0; i < passwordlsList.Count - 1; i++)
{
oUserPrincipal.ChangePassword(passwordlsList[i], passwordlsList[i + 1]);
Console.WriteLine($"Pass change from \"{passwordlsList[i]}\" to \"{passwordlsList[i + 1]}\"");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment