Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically change my windows domain password?

In other words, how to change my password without going through the "Ctrl+Alt+Del -> Change Password" interface.

By programmatically I mean via a command-line tool, C# via a .NET library, COM-invocation via Python, ... Whatever doesn't involve any manual steps, really.

The NET USER command is ineligible, as it requires me to run with domain administrator privileges.

like image 354
Deestan Avatar asked Jun 08 '10 11:06

Deestan


2 Answers

Use the DirectoryEntry class to get and update the active directory entry for the user.

http://linuxonly.nl/docs/21/43_Circumvent_password_expiry_in_Windows.html

like image 140
Sjoerd Avatar answered Nov 17 '22 04:11

Sjoerd


Here is a modified version of the code Sjoerd provided that changes the password once rather than cycling through multiple password changes.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;

namespace ChangePassword
{
    class Program
    {
        static void Main(string[] args)
        {
            string Domain = Environment.UserDomainName;
            string User = Environment.UserName;

            if (args.Length < 2)
            {
                System.Console.WriteLine("Usage: ChangePassword OldPassword NewPassword [User]");
                System.Console.WriteLine("       -The domain is " + Domain + ".");
                System.Console.WriteLine("       -The user is " + User + " unless it is specified.");
                System.Environment.Exit(1);
            }
            string OldPassword = args[0];
            string NewPassword = args[1];
            if (args.Length == 3)
                User = args[2];

            DirectoryEntry entry = null;
            try {
                entry = new DirectoryEntry(@"WinNT://" + Domain + "/" + User + ",User");
            }
            catch (System.Reflection.TargetInvocationException e)
            {
                System.Console.WriteLine("Domain/User failed due to:");
                Exception cause = e.InnerException;
                System.Console.WriteLine(cause.Message);
                System.Environment.Exit(1);
            }

            try {
                entry.Invoke("ChangePassword", OldPassword, NewPassword);
            }
            catch (System.Reflection.TargetInvocationException e)
            {
                System.Console.WriteLine("Password change failed due to:");
                Exception cause = e.InnerException;
                System.Console.WriteLine(cause.Message);
                System.Environment.Exit(1);
            }
            System.Console.WriteLine("Ok.");
        }
    }
}
like image 22
Leigh Riffel Avatar answered Nov 17 '22 05:11

Leigh Riffel