Unlock, Enable and Reset Password on a User Account in Active Directory via Script

JSscript that unlocks a User account in AD using the WinNT Provider.
Save as “UnlockAccount.js” or whatever you prefer and run “cscript UnlockAccount.js [Domain] [UserName] [Optional:Password]
Takes 2 Arguments (Domain Name, UserName,Optional:NewPassword)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
UnlockAccount( WScript.Arguments(0), WScript.Arguments(1));
function UnlockAccount( domain, account)
{
  try 
  {
    var user = GetObject("WinNT://" + domain + "/" + account);
 
    if( user.AccountDisabled)
    {
      user.AccountDisabled = false;
      user.SetInfo();
      WScript.Echo("Account Enabled");
    }
    else  
    {
      WScript.Echo("Account was Enabled" );  
    }
 
    if( user.IsAccountLocked )  
    {
      user.IsAccountLocked = false;
      user.SetInfo();
      WScript.Echo("Account Unlocked");
    }
    else  
    {
      WScript.Echo("Account was not locked" );  
    }
 
 
    if (WScript.Arguments.Count() == 3)
    {
      user.SetPassword(WScript.Arguments(2));
      WScript.Echo("Password Updated");
    }
    else
    {
      WScript.Echo("Password not changed");
    }
  }
  catch( e ) 
  {
      WScript.Echo( "Error: " + e.description ); 
  }
}

Leave a Reply