How to Recycle Remote IIS Application Pool from Command Line Using PowerShell and WinRM


1 views

When managing IIS servers across multiple machines, administrators often need to recycle application pools remotely. While appcmd.exe works well locally, executing it remotely requires additional tools and configuration.

Before attempting remote IIS management, ensure:

  • WinRM is enabled on both machines
  • You have administrative privileges
  • Firewall allows WinRM traffic (default port 5985)

Run this command on the target server (Server 2008/R2):

winrm quickconfig

For Server 2012 and later, use:

Enable-PSRemoting -Force

Here's a complete PowerShell script to recycle an app pool remotely:

# Define connection parameters
$computerName = "remote-server-name"
$appPoolName = "YourAppPoolName"
$credential = Get-Credential

# Establish remote session
$session = New-PSSession -ComputerName $computerName -Credential $credential

# Execute recycling command
Invoke-Command -Session $session -ScriptBlock {
    Import-Module WebAdministration
    Restart-WebAppPool -Name $using:appPoolName
}

# Clean up
Remove-PSSession $session

For environments where PowerShell remoting isn't available, you can use WinRS:

winrs -r:remote-server-name -u:username -p:password "appcmd recycle apppool YourAppPoolName"

If you encounter "Access Denied" errors:

  1. Ensure the account has IIS Administrator privileges
  2. Check User Account Control (UAC) settings
  3. Verify WinRM service is running

For regular maintenance, create a scheduled task that runs this PowerShell script:

$action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "-NoProfile -ExecutionPolicy Bypass -File C:\Scripts\RecycleAppPool.ps1"
$trigger = New-ScheduledTaskTrigger -Daily -At 2am
Register-ScheduledTask -TaskName "Nightly AppPool Recycling" -Action $action -Trigger $trigger

When implementing remote management:

  • Use HTTPS for WinRM instead of HTTP
  • Implement IP restrictions
  • Consider using certificate-based authentication
  • Log all remote management activities

Recycling application pools affects:

Factor Impact
Active Sessions Terminated
Memory Cleared
Application Startup Required

When managing IIS servers across multiple machines, you need proper remote administration tools. For IIS7+ environments, you have several technical approaches:

# Local app pool recycle command
appcmd recycle apppool "MyAppPool"

The most robust solution involves PowerShell remoting through WinRM (Windows Remote Management). First ensure WinRM is configured on both machines:

# Enable WinRM on target server (run as Administrator)
winrm quickconfig
Enable-PSRemoting -Force

# Configure trusted hosts on client machine
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "ServerName" -Force

Once configured, you can recycle app pools remotely using PowerShell:

# Method 1: Invoke-Command
Invoke-Command -ComputerName Server01 -ScriptBlock {
    Import-Module WebAdministration
    Restart-WebAppPool -Name "MyAppPool"
}

# Method 2: Enter-PSSession (interactive)
Enter-PSSession -ComputerName Server01
Restart-WebAppPool "MyAppPool"
Exit-PSSession

For environments where WinRM isn't available, consider these alternatives:

# Using WMI (Windows Management Instrumentation)
$appPool = Get-WmiObject -Namespace "root\MicrosoftIISv2" -Class "IIsApplicationPool" -ComputerName Server01 -Filter "Name='W3SVC/APPPOOLS/MyAppPool'"
$appPool.Recycle()

# Using psexec from Sysinternals
psexec \\Server01 appcmd recycle apppool "MyAppPool"

Always implement proper error handling for production scripts:

try {
    $result = Invoke-Command -ComputerName Server01 -ScriptBlock {
        Import-Module WebAdministration
        Restart-WebAppPool -Name "MyAppPool" -ErrorAction Stop
    }
    Write-Host "Successfully recycled app pool on Server01"
}
catch {
    Write-Error "Failed to recycle app pool: $_"
}

For automated recycling scenarios, consider creating a scheduled task:

# Create scheduled task XML
$action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-Command "Import-Module WebAdministration; Restart-WebAppPool 'MyAppPool'""
$trigger = New-ScheduledTaskTrigger -Daily -At 2am
Register-ScheduledTask -TaskName "Nightly AppPool Recycle" -Action $action -Trigger $trigger -RunLevel Highest