Windows Scripting: Working with services

Although there are PowerShell cmdlets to drive services, most of the examples seem to use WMI underneath. Having recently had the misfortune of changing every service’s log on details to my account, and having to set them back, I came across a few where the service’ log on settings were greyed out. Odd considering I had been able to change them in the first place, albeit via a script.

The above script will iterate through a list of servers in the $servers array and then iterate through the services looking for any service running under the given account(s) specified in the $accountLookup array. You will notice the account in both formats. Finally, in line 42,  it changes the log on as credentials. In my case, the username is the same for the search and the change and all I was doing was updating the password.

If you are working with domain accounts, the 6th parameter has to be $false, (line 6) otherwise for local accounts it should be $null.

If you are setting a service to “Local Service” $newAcct should be set to “NT AUTHORITY\localservice and for “Local System” $newAcct should be set to “LocalSystem”. Any other local account needs to be preceded with .\. As in “.\LocalAccount”.

To change just one service locally on onemachine, you need to use lines 3 and 6 in a PowerShell command window. Obviously changing the variables accordingly

To change Plug and Play (typically greyed out) service:


A ReturnValue of 0 is a success. Return errors can be partially deciphered using:





Please feel free to leave a comment...