business.com receives compensation from some of the companies listed on this page. Advertising Disclosure

Home

How To Manage IIS Application Pools With PowerShell

Adam Bertram
business.com Contributing Writer
Sep 06, 2018

Where you turn when you're managing an IIS web server. farm with potentially dozens or h

Where do you turn when you're managing an IIS web server farm with potentially dozens or hundreds of app pools? PowerShell, of course.

By using the WebAdministration PowerShell module that comes installed as part of IIS and a little PowerShell remoting you can easily create, modify and remove app pools at will.

How to make that happen.

If you've never used PowerShell to manage your IIS servers before your first inclination might be to look for a –ComputerName on most of the cmdlets. Unfortunately, this is not the case. In order to manage IIS servers remotely, we're forced to use PowerShell remoting with the Invoke-Command cmdlet. Although that's not a deal-breaker, it does make the code a little more verbose than what it could be. It can be pretty frustrating the first time you try this as you won't see that familiar –ComputerName parameter on many of the cmdlets.

NOTE: Going forward, we'll be building code to input into a scriptblock. We'll then use Invoke-Command to execute this scriptblock on the remote IIS server.

To manage application pools, we'll first need to import the WebAdministration module.

Import-Module WebAdministration

This brings in all of the IIS cmdlets, and creates the IIS drive. This is where most of the app pool configuration will be done. Let's first check to see if any app pools already exist.

Get-ChildItem –Path IIS:\AppPools

It looks like we have one called GHI already; maybe we want to create another one. Using the IIS drive makes this very easy. Simply use New-Item and specify the path.

New-Item –Path IIS:\AppPools\MyAppPool

We have now created a new app pool. We can then check all of the properties on that app pool using Get-ItemProperty and select all of the properties it returns with Select-Object. This will return all of the property names and values so you can figure out which ones you need to modify with Set-ItemProperty.

Get-ItemProperty IIS:\AppPools\MyAppPool | select *

Now that you've got an app pool and can see the properties, let's modify a property. Maybe we want to use a specific .NET runtime version with the app pool. Again using the IIS drive we can use Set-ItemProperty to manage app pools like we can the file system, registry, certificates and all the other things that have a PowerShell drive.

Set-ItemProperty -Path IIS:\AppPools\MyAppPool -Name managedRuntimeVersion -Value 'v4.0'

By using Set-ItemProperty you can modify nearly all of the properties for an app pool.

Finally, we're done with our app pool and now need to remove it. This time we have a built-in PowerShell cmdlet called Remove-WebAppPool. Simply specify the name and it's gone! 

Remove-WebAppPool -Name MyAppPool

All of this code we've been using was all executed locally, but what if you need to run it on a remote IIS server? This is where PowerShell remoting comes in. To do this, we'll simply need to bundle all of this code in a scriptblock and then use Invoke-Command to execute it on the remote server. 

$appPoolName = 'MyAppPool'
$scriptBlock = {
    Import-Module WebAdministration
    New-Item –Path IIS:\AppPools\$using:appPoolName
    Set-ItemProperty -Path
    IIS:\AppPools\$using:appPoolName -Name
    managedRuntimeVersion -Value 'v4.0'
    Remove-WebAppPool -Name $using:appPoolName
}
Invoke-Command –ComputerName SOMEIISSERVER –ScriptBlock $scriptBlock

Bottom line

Although it's not very functional, this code would create a new app pool named MyAppPool, set a property and then remove it. You'll notice we are using the $using variable. Since the code in the script block is going to be executing on a remote computer, this is necessary in order for PowerShell to expand that variable and to use the actual value of $appPoolName that was declared locally on our client computer.

If you'd like to learn more about managing IIS in general check out the Technet IIS Administration page. There you'll find all of the cmdlets included in the WebAdministration module and how you can use them.

Image Credit: Undrey/Shutterstock
Adam Bertram
business.com Contributing Writer
Adam Bertram is a 20-year veteran of IT and experienced online business professional. He's an entrepreneur, IT influencer, Microsoft MVP, blogger, trainer and content marketing writer for multiple technology companies. Adam is also the founder of the popular IT career development platform TechSnips. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram or the TechSnips Twitter account at @techsnips_io.