简体   繁体   中英

Programmatically disable Password Complexity in Windows

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.

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM