Change Remote Windows Service Credentials and Password in Powershell

The following function will:

Connect to the service that it’s trying to change credentials on through WMI

Change the credentials to those provided when calling the function

Stop the service, wait until the service is stopped (while re-enumerating the service to get up to date WMI information)

… and then start the service again.

If you have any issues with the script, feel free to comment below and I will work to add some error handling or additional functionality. Also, WMI can sometimes hang when a server is in a bad state. If this happens please look for a custom WMI function with connection options specified. If someone needs that, again – comment and I will post some sample code.

As always, enjoy!

Function Set-ServiceAcctCreds([string]$strCompName,[string]$strServiceName,[string]$newAcct,[string]$newPass)<br /> {<br /> $filter = 'Name=' + "'" + $strServiceName + "'" + ''<br /> $service = Get-WMIObject -ComputerName $strCompName -namespace "root\cimv2" -class Win32_Service -Filter $filter<br /> $service.Change($null,$null,$null,$null,$null,$null,$newAcct,$newPass)<br /> $service.StopService()<br /> while ($service.Started){<br /> sleep 2<br /> $service = Get-WMIObject -ComputerName $strCompName -namespace "root\cimv2" -class Win32_Service -Filter $filter}<br /> $service.StartService()<br /> }


Usage: Set-ServiceAcctCreds -strCompName “Computer1″ -strServiceName “Service” -newAcct “DOM\ServiceUser” -newPass ‘newSecureWord’