Forum Discussion
Service control manager cached credentials
I believe SC.exe and/or WMI should be able to update the password.
Hi, Animesh.
As you've said, you can look to calling an external application such as sc.exe or leverage WMI.
If you're really keen on a native PowerShell approach, and in the specific context of an approach that will work with version 4.0, you'd be looking to make use of the platform invoke (commonly shortened to "p/invoke") method.
Here's a good example on stackoverflow for using the p/invoke approach specifically with Windows services.
Eventually, you'd be looking to leverage the ChangeServiceConfigW function to set the password. Most of the other parameters can simply be left as null, so it's not as bad as it looks.
c# - change windows service password - Stack Overflow
You'd be wise to wrap this in a static class definition but it's not strictly necessary.
Cheers,
Lain