Powershell: script to replace credentials to windows services

Introduction

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 after completion. If the service is not running before the change, the service will not be started after.

Prerequisites

  1. The ps1 file should be executed elevated. If not the change method will fail.
  2. The account running the script must have:

Functionalities

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

​Parameters

    • 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 where you want to change credentials. WildCards characters are supported since the query is filtering using a like expression. Examples:
      • ‘MSSQL’–> it will act over all SQL Server processes in the server
      • ‘BTSSVC%’  –> it will act over all BizTalk processes in the server
    • ServerN: Name of the server where you want to change the service.
    • SecondsToWait: This is the time in seconds that the script will wait before starting the service again. Default is 1. 

Output

The following screenshot shows you the output of the script:

Examples

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

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

 

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

    .\changeUser.ps1 –UserName .\Administrator –Password admPwd –Service ‘SQLSERVERAGENT‘ –ServerNServerA –SecondsToWait 2
    

Download the code

You can download the code from here (MSDN Code Gallery)