Click here to Skip to main content
13,090,091 members (43,981 online)
Click here to Skip to main content
Add your own
alternative version


35 bookmarked
Posted 13 Mar 2012

How to Change User Credentials of Windows Services Programmatically

, 6 Sep 2012
Rate this:
Please Sign up or sign in to vote.
This tip describes how to change username/password for a Windows service programmatically.


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 tip will demonstrate how to do that for this problem.

Above is the UI for this utility where the user needs to provide a 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.


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


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


About the Author

Architect Aptean
India India
Pankaj Shukla is currently working at Aptean(formerly CDC Software), Bangalore as Associate Architect(DEV).
He is a B.Tech. in Comp. Engg. from GB Pant University, Pantnagar.
Pankaj has been mostly a programmer on Windows platform. His programming interests are Web Technologies(HTML5, JavaScript, CSS), ASP.NET MVC, C++, C#.NET applications.
He can be contacted at

You may also be interested in...


Comments and Discussions

QuestionBypass logon screen Pin
Aster Veigas12-Mar-13 2:07
memberAster Veigas12-Mar-13 2:07 
QuestionDownload Problem Pin
e.t. in the lab13-Aug-12 8:05
membere.t. in the lab13-Aug-12 8:05 
AnswerRe: Download Problem Pin
Pankaj_Shukla6-Sep-12 2:40
memberPankaj_Shukla6-Sep-12 2:40 
AnswerRe: Download Problem Pin
Pankaj_Shukla6-Sep-12 2:41
memberPankaj_Shukla6-Sep-12 2:41 
Questiondownload problem Pin
andrey91926-Mar-12 7:50
memberandrey91926-Mar-12 7:50 
AnswerRe: download problem Pin
Pankaj_Shukla6-Sep-12 2:39
memberPankaj_Shukla6-Sep-12 2:39 
Questionusing System.Management; Pin
Pradeep4u212-Mar-12 4:19
memberPradeep4u212-Mar-12 4:19 
GeneralMy vote of 5 Pin
Arnold Sonico29-Sep-10 13:11
memberArnold Sonico29-Sep-10 13:11 
GeneralRe: My vote of 5 Pin
Pankaj_Shukla10-Dec-10 1:57
memberPankaj_Shukla10-Dec-10 1:57 
GeneralOr... Pin
PIEBALDconsult17-Feb-09 5:54
memberPIEBALDconsult17-Feb-09 5:54 
GeneralRe: Or... Pin
Pankaj_Shukla26-Feb-09 7:11
memberPankaj_Shukla26-Feb-09 7:11 
GeneralRe: Or... Pin
Donsw5-Mar-09 8:49
memberDonsw5-Mar-09 8:49 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

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