Wednesday, 4 March 2015

Powershell - Change Service Account Username and Password

In Powershell, we can change Windows Service Account username and password using WMI based powershell cmdlet Get-WmiObject and the WMI class Win32_Service. You can even easily change Service account infromation in Remote Computer.

Powershell: Change Service Account username and password

Use below sample powershell script to change service account username and password. Before using this script, change the user credentials and service name with your own values in the script.
# Change service user name and password 
# www.morgantechce.com
#
$UserName = "TestDomain\Morgan"  
$Password = "MyPa55w0rd" 
$Service = "TestService" #Change your own service name
$svc_Obj= Get-WmiObject Win32_Service -filter "name='$service'"
$StopStatus = $svc_Obj.StopService() 
If ($StopStatus.ReturnValue -eq "0") 
    {Write-host "The service '$Service' Stopped successfully"} 
$ChangeStatus = $svc_Obj.change($null,$null,$null,$null,$null,
                      $null, $UserName,$Password,$null,$null,$null)
If ($ChangeStatus.ReturnValue -eq "0")  
    {Write-host "User Name sucessfully for the service '$Service'"} 
$StartStatus = $svc_Obj.StartService() 
If ($ChangeStatus.ReturnValue -eq "0")  
    {Write-host "The service '$Service' Started successfully"} 

Powershell: Change Service user name and password in Remote Server

Use below sample powershell script to change service user name and password from remote computer. Before using this script, change the user credentials, remote server name and service name with your own values in the script.
# Change service username and password in Remote Computer
# www.morgantechspace.com
#
$UserName = "TestDomain\Morgan"  
$Password = "MyPa55w0rd" 
$Service = "TestService" #Change your own service name
$computer = "Your-Remote-PC" #Change your own server/computer name
#Prompt you for user name and password to access remote computer 
$Cred = Get-Credential 
$svc_Obj= Get-WmiObject Win32_Service -ComputerName $computer -filter "name='$service'" -Credential $cred
$StopStatus = $svc_Obj.StopService() 
If ($StopStatus.ReturnValue -eq "0") 
    {Write-host "The service '$Service' Stopped successfully in $computer"} 
$ChangeStatus = $svc_Obj.change($null,$null,$null,$null,$null,
                    $null,$UserName,$Password,$null,$null,$null)
If ($ChangeStatus.ReturnValue -eq "0")  
    {Write-host "User Name sucessfully for the service '$Service' in $computer"} 
$StartStatus = $svc_Obj.StartService() 
If ($ChangeStatus.ReturnValue -eq "0")  
    {Write-host "The service '$Service' Started successfully in $computer"} 

Advertisements
Advertisements

No comments:

Post a Comment