Programmatically disable Password Complexity in Windows

1k Views Asked by At

I have spent all day looking for an answer but without luck.

I need to be able to disable the Password Complexity in the Local Security Policy on a stand-alone Windows 7 PC.

I have tried scripting with secedit.exe. I have also messed a bit around with C#.

The end result shall be a script/program which will disable the policy and then create a new user account locally.

1

There are 1 best solutions below

0
On BEST ANSWER

After some extended research I found out how to do it. Posting the code here in case someone else needs to use it.

string tempFile = Path.GetTempFileName();
Process p = new Process();
p.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\secedit.exe");
p.StartInfo.Arguments = String.Format("/export /cfg \"{0}\" /quiet", tempFile);
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.Start();
p.WaitForExit();

StringBuilder newCfg = new StringBuilder();
string[] cfg = File.ReadAllLines(tempFile);

foreach (string line in cfg)
{
    if (line.Contains("PasswordComplexity"))
    {
        newCfg.AppendLine(line.Replace("1", "0"));
        continue;
    }
    newCfg.AppendLine(line);
}
File.WriteAllText(tempFile, newCfg.ToString());

Process p2 = new Process();
p2.StartInfo.FileName = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\system32\secedit.exe");
p2.StartInfo.Arguments = String.Format("/configure /db secedit.sdb /cfg \"{0}\" /quiet", tempFile);
p2.StartInfo.CreateNoWindow = true;
p2.StartInfo.UseShellExecute = false;
p2.Start();
p2.WaitForExit();