Reset username and password of IIS Server Application Pool

There are chances where we need to get Application Pools user name and password for further modifications such as refreshing account or changing password for the users.

Below PowerShell Script will loop through all the App Pools  (excluding defaults) and will reset the same user and password again. Scripts can be easily customized to use to update password when a change occur.

# Check if appcmd.exe exists in default path
if (Test-Path  ("c:\windows\system32\inetsrv\appcmd.exe")) {    

	# Set AppCmd.exe path in variable for further usage.
    $AppCmdPath = 'c:\windows\system32\inetsrv\appcmd.exe'
	
    # Get list of application pools
    & $AppCmdPath list apppools /text:name | 
        ForEach-Object { 
                    
        #Get application pool name
        $PoolName = $_
        
        #Exclude inbuild Application Pools
        if(
        $PoolName -eq "DefaultAppPool" -Or 
        $PoolName -eq "Classic .NET AppPool" -Or 
        $PoolName -eq ".NET v2.0 Classic"-Or
        $PoolName -eq ".NET v2.0"-Or 
        $PoolName -eq ".NET v4.5 Classic" -Or
        $PoolName -eq ".NET v4.5"
        ){
            Write-Host  "Inbuild Pool" + $PoolName
        }
        else{
            #Get username                   
            $PoolUserCmd = $AppCmdPath + ' list apppool "' + $PoolName + '" /text:processmodel.username'
            $PoolUser = invoke-expression $PoolUserCmd 
                                          
            #Get password
            $PoolPasswordCmd = $AppCmdPath + ' list apppool "' + $PoolName + '" /text:processmodel.password'
            $PoolPassword = invoke-expression $PoolPasswordCmd 

            #Check if credentials exists
            if ($PoolPassword -ne "") {
                           
                #Re-set the app pool with the same credentials             
                & $AppCmdPath set config -section:system.applicationHost/applicationPools "/[name='$($PoolName )'].processModel.identityType:SpecificUser" "/[name='$($PoolName)'].processModel.userName:$($PoolUser)" "/[name='$($PoolName)'].processModel.password:$($PoolPassword)" 
            }
        }        
    }
    # Do IISRESET after re-setting passwords
    & {iisreset}   
}
else {
    Write-Host -Object 'Could not find the appcmd.exe path at default location, please try at different place.'
}

Please note that PowerShell script should be executed in Administrative Privilege.

Script is available here


2 Comments

Add a Comment