Reset IIS on all servers in a farm

How many times have you been in the middle of a series of updates to your farm, or at the end of an update, or maybe trying to resolve an issue with the magic of IIS reset and felt the pain of:

  • Logging into each server
  • Opening a command prompt
  • Typing IISRESET /noforce
  • and then wait, make sure all are done, etc.

We’ll no longer shall you have to do that miserable task of logging in etc. And let the record show, this is not a new script, I didn’t even write it, it’s probably out there on other blogs, I got this from a fellow SharePoint guru.

############################################################################################################################################ 
# This Script allows you to do an IIS RESET to all the servers in a SharePoint Farm 
# Requited parameters: N/A 
############################################################################################################################################ 
If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )  
{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell } 
 
$host.Runspace.ThreadOptions = "ReuseThread" 
 
#Definition of the funcion that performs the IIS RESET in all the servers 
function Do-IISReset 
{     
    try 
    {         
        #Getting the servers where the IISReset is going to be done 
        $spServers= Get-SPServer | ? {$_.Role -eq "Application"} 
        foreach ($spServer in $spServers) 
        {             
            Write-Host "Doing IIS Reset in server $spServer" -f blue 
            iisreset $spServer /noforce "\\"$_.Address 
            iisreset $spServer /status "\\"$_.Address 
        }         
        Write-Host "IIS Reset completed successfully!!" -f blue   
    } 
    catch [System.Exception] 
    { 
        write-host -f red $_.Exception.ToString() 
    } 
} 
 
Start-SPAssignment –Global 
#Calling the function 
Do-IISReset 
Stop-SPAssignment –Global 
 
Remove-PSSnapin Microsoft.SharePoint.PowerShell

Here’s how you use it:

Create a powershell script

  •  copy the above into a text file and rename the file with a .ps1 extension

Run the powershell script

  • open the SharePoint Management shell, navigate to wherever you saved the .ps1 file, and type “.\filename.ps1” where filename is the name you used, and omit the quotes.

If you’re running it for a SharePoint 2016 farm, you’ll want to modify line 16, else it will only reset the app server.

############################################################################################################################################ 
# This Script allows you to do an IIS RESET to all the servers in a SharePoint Farm 
# Requited parameters: N/A 
############################################################################################################################################ 
If ((Get-PSSnapIn -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null )  
{ Add-PSSnapIn -Name Microsoft.SharePoint.PowerShell } 
 
$host.Runspace.ThreadOptions = "ReuseThread" 
 
#Definition of the funcion that performs the IIS RESET in all the servers 
function Do-IISReset 
{     
    try 
    {         
        #Getting the servers where the IISReset is going to be done 
        $spServers= Get-SPServer  
        foreach ($spServer in $spServers) 
        {             
            Write-Host "Doing IIS Reset in server $spServer" -f blue 
            iisreset $spServer /noforce "\\"$_.Address 
            iisreset $spServer /status "\\"$_.Address 
        }         
        Write-Host "IIS Reset completed successfully!!" -f blue   
    } 
    catch [System.Exception] 
    { 
        write-host -f red $_.Exception.ToString() 
    } 
} 
 
Start-SPAssignment –Global 
#Calling the function 
Do-IISReset 
Stop-SPAssignment –Global 
 
Remove-PSSnapin Microsoft.SharePoint.PowerShell

Cheers and Happy SharePointing!!!