PowerShell Quick Tip: Restart App Pool Remotely

PowerShell has become my “go to” tool for administering or troubleshooting remote servers.  It is so nice to not have to remote into the server with Remote Desktop.

This usually starts with a quick google search to find a sample script to perform the needed task in PowerShell.  Over time I have accumulated a decent list of common administration tasks in PowerShell.

Restarting a remote AppPool has always been a common need but I’ve had mixed results using the standard get-item cmdlet.  I often received the following highlighted error:

> invoke-command -ComputerName TargetServer1 -ScriptBlock { import-module WebAdministration; get-item “IIS:\Sites\myApp*” }


The data is invalid. (Exception from HRESULT: 0x8007000D)

     + CategoryInfo          : NotSpecified: (:) [Get-Item], COMException
     + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException,Microsoft.PowerShell.Commands.GetItemCommand
     + PSComputerName        : TargetServer1

With a little help I found I needed to pipe the result to a format-list cmdlet inside the scriptblock to avoid the COM exception.

> invoke-command -ComputerName TargetServer1 -ScriptBlock { import-module WebAdministration; get-item “IIS:\Sites\myApp*” | Format-List}

Once I find the exact name of the AppPool I can then use the start-AppPool or restart-AppPool cmdlets to start it up:

> invoke-command -ComputerName TargetServer1 -ScriptBlock { import-module WebAdministration; $pool = get-item “IIS:\Sites\myAppPool1” | Select-Object applicationPool; Restart-WebAppPool $pool.applicationPool}

Happy remote administering!

Leave a Reply