Powershell script to change windows service credentials

 

With this powershell script you will be able to change the user name and password of any Windows service. It can work against 1 or more services at the same time. The process will stop the service before changing the credentials and it will start it aftet. If the service is not running before the change, the service will not be started neither.

 

Pre-Requisites:

  1. The ps1 file should be executed elevated. If not the change method will fail.
  2. The account running the script must have:
    • Rights to execute powershell Sonrisa
    • WMI Access (since we are accessing the win32_service class)


Functionalities

  • You can change the user name and password of 1 windows service or many.
  • it can interact with a remote server.


Parameters

All parameters are mandatory. If you don’t provide them, powershell will ask you to provide them.

  • UserName: This would be the new user name. Examples:
    • .\Administrator
    • DomainName\Administrator
  • Password: this is the user password. If the password contains a $ you must pass the parameter enclosed in ‘’ (the symbol $ is not allowed in ps)
  • Service: This is the service name in which you want to change the credentials. WildCards characters are supported since the query is filtering using a like expression. Examples:
    • ‘MSSQLSERVER’
    • ‘BTSSVC%’  --> it will act over all BizTalk porcesses in the server
  • ServerN: Name of the server in which we want to change the service.
  • SecondsToWait: This is the time in seconds that the script will wait before starting the service again


Output:

This is an output example:

image

This was an attempt to change the user and password of all Windows services starting by MSSQL.

 

Examples:

  • Changing the user and pasword of all BizTalk host instances:

    .\changeUser.ps1 -UserName .\Administrator -Password admPwd -Service 'BTSSVC%' –ServerN ServerA -SecondsToWait 2

 

  • Changing the user and pasword of the SQL Server Agent:

    .\changeUser.ps1 -UserName .\Administrator -Password admPwd –Service ' SQLSERVERAGENT' –ServerN ServerA –SecondsToWait 2

Download the Code

You can download the code here, at MSDN Code Gallery.