Click here to Skip to main content
15,881,832 members
Articles / Programming Languages / PowerShell
Tip/Trick

Powershell To update Services Password, Sharepoint Managed Acount Password and IIS AppPool Password

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
21 Jul 2014CPOL 11.4K   2  
Update Password using PowerShell

Powershell To update Services Password, Sharepoint Managed Acount Password and IIS AppPool Password

Many a times there is a need to update the changed password in Virtual Machines. It can be done manually. But doing it via Powershell will save us a lot of time.

Background

After browsing for a while, I couldn't find an article that pointed me in doing all the above using one script. So I thought to share the powershell comands that I just put together, which can be optimized/refactored by admins to use them in prod environment.

Using the code

PowerShell
//
// Save the below powershell code as ps1 and run in Powershell
// Update Domain, UserName and Password with your actual values
//

$localaccount = "domain\username"
$newpassword = "************"

#Set Execution Policy
Set-ExecutionPolicy -ExecutionPolicy Unrestricted -force

#Import SharePoint Module
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA silentlycontinue

#Update Service Account
Write-Host "Updating Password for Services" -foregroundcolor green
$services = Get-WmiObject win32_Service | where { $_.startname -eq $localaccount}

foreach ($service in $services)
{  
    #Update Password
    Write-Host "Updating Password." -foregroundcolor 13
    $status = $service.Change($null,$null,$null,$null,$null,$null, $localaccount, $newpassword)
    Write-Host "Status:  " $status.ReturnValue -foregroundcolor 13	
    
}

#Stop Service
Write-Host "Stop all running Services." -foregroundcolor green
Get-WmiObject win32_Service | where { ($_.startname -eq $localaccount -And $_.state -ne "Stopped" -And  $_.startmode -ne "Disabled") } | stop-service -force

#Start Service
Write-Host "Restarting stopped Services" -foregroundcolor green
Get-WmiObject win32_Service | where { ($_.startname -eq $localaccount -And $_.state -ne "Running" -And  $_.startmode -ne "Disabled") } | start-service

#Update Sharepoint Managed Account
$ManagedAccount = Get-SPManagedAccount -Identity $localaccount
$securePassword = ConvertTo-SecureString -String $newpassword -AsPlainText -Force

Write-Host "Please wait while Passwod for SharePoint Managed Account gets updated." -foregroundcolor red

Set-SPManagedAccount -Identity $ManagedAccount -ExistingPassword $securePassword -Confirm:$false

Write-Host "Password for SahrePoint Updated Successfully" -foregroundcolor green

#Update App Pool Accounts
Import-Module WebAdministration

#Restart IIS
Write-Host "Restarting IIS. " -foregroundcolor green
iisreset /noforce

$appPools = Get-ChildItem IIS:\appPools  | where {$_.state -ne "Started"}

foreach ($appPool in $appPools) 
{    
    if ($appPool.processModel.userName -eq $localaccount)
    {
        $appPoolName = $appPool.Name
        $appPool.processModel.password =  $newpassword	
        $appPool | Set-Item
	$appPool.Recycle()
        Write-Host "Password for AppPool " $appPoolName " updated successfully"  -foregroundcolor green
    }
}

#Restart IIS
Write-Host "Restarting IIS after password reset. " -foregroundcolor green
iisreset /noforce


Write-Host "IIS Restarted Successfully. " -foregroundcolor green

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) 3MD Tech
United States United States
Rocky Fernandes has over 11 years of experience in Software Industry mostly developing Web Applications. Other than having vast experience in various Microsoft Technologies like SharePoint, ASP.Net, C#, VB.Net, Silverlight, WCF, MVC, Web API, SQL Server, ADO.Net, ADOMD.Net, LINQ, Entity Framework, Exchange Web Service(EWS) API, he also has vast experience in Client Side Scripting like JavaScript, jQuery, JSON, JSONP, CSS and HTML5. He is very much passionate in programming and enjoys doing it. In the academic side he holds a BE Degree in Computer Science & Engineering. In the certification side he holds certifications in Sharepoint 2010, SharePoint 2013, ASP.Net & SQL Server.

In most of the free time, he will be spending in technical activities like researching solutions, reading technical articles and learning new stuff. He believes that following Best Practices and Coding standard is a way to go for producing an efficient, quality & satisfactory application.

Homepage: http://www.smartsolutionshub.com/

Comments and Discussions

 
-- There are no messages in this forum --