Click here to Skip to main content
Click here to Skip to main content

How to Change User Credentials of Windows Services Programmatically

By , 6 Sep 2012
 

Introduction

I have some Windows services that run under my domain account credentials. When my password expires and I change it, my services generate a log-on failure due to old password. Now I have to change the password for each and every service running under my account. Worse, I have to type the password twice for each service. I just thought there could be a better way to do this and thought of writing this small utility. This utility sets the password for all the services running under my account and I manage to save some keyboard key hits! This utility uses WMI features using C# and .NET. .NET really makes manipulating Windows objects simple and this article will demonstrate how to do that for this problem.

Above is the UI for this utility where the user needs to provide new password due to changed log-on details.

Using the Code

Below is a brief description of the code as to how it works:

//Use the following namespaces in your file
using System.Management; // This is needed for manipulating services
using System.Security.Principal;//This is needed for logged-in user name
//Below we are using WQL querying capability. 
//This is similar to SQL with a difference that we use it to query Windows objects      
string queryStr = "select * from Win32_Service where StartName like '";
queryStr += uName.Replace('\\', '%');//Here uName is in domain\\password format
queryStr += "'";
ObjectQuery oQuery = new ObjectQuery(queryStr);

//Execute the query  
ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oQuery);

//This will give collection of all the services running under user name account
ManagementObjectCollection oReturnCollection = oSearcher.Get();		

Once we are done with collecting all the services, we can iterate over them to change the password as shown below:

foreach (ManagementObject oReturn in oReturnCollection)
{
  string serviceName = oReturn.GetPropertyValue("Name") as   string;

  string fullServiceName = "Win32_Service.Name='";
  fullServiceName += serviceName;
  fullServiceName += "'";

  ManagementObject mo = new ManagementObject (fullServiceName);
  //This will change the credentials for the service
  mo.InvokeMethod("Change", new object[] 
	{ null, null, null, null, null, null, uName, passWord, null, null, null });

  //This will start the service
  mo.InvokeMethod("StartService", new object[] { null });
}

Points of Interest

It was interesting to know how .NET makes WMI so simple to use. Also, this utility is more helpful if there are more services that are needed by the user and the frequency of password change is high. Otherwise, this could be used as a tutorial on how to do this kind of stuff.

History

  • 17th February, 2009: First version of the article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Pankaj_Shukla
Software Developer (Senior)
India India
Member
Pankaj Shukla is currently working at CDC Software, Bangalore as a Tech. Lead.
He is a B.Tech. in Comp. Engg. from GB Pant University, Pantnagar.
Pankaj has been mostly a C++ programmer on Windows platform . His programming interests are writing C++, C#.NET applications.
He can be contacted at pankaj_shukla369@yahoo.com.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionBypass logon screenmemberAster Veigas12 Mar '13 - 2:07 
QuestionDownload Problemmembere.t. in the lab13 Aug '12 - 8:05 
AnswerRe: Download ProblemmemberPankaj_Shukla6 Sep '12 - 2:40 
AnswerRe: Download ProblemmemberPankaj_Shukla6 Sep '12 - 2:41 
Questiondownload problemmemberandrey91926 Mar '12 - 7:50 
AnswerRe: download problemmemberPankaj_Shukla6 Sep '12 - 2:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 6 Sep 2012
Article Copyright 2012 by Pankaj_Shukla
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid