How do I set the .NET Framework Version when using New-WebAppPool?

WindowsIisPowershellApplication Pool.Net Framework-Version

Windows Problem Overview


I'm looking to see how I can use the IIS PowerShell Cmdlet New-WebAppPool to specify the version of the .NET Framework to use. Currently, it defaults to v2.0, however I am using MVC, and this will not work because that's a v4.0 feature. We really want each site to have its own Application Pool, and it seems we must create those pools manually due to the inability to configure them via script. Is there any way to automate this?

I'm afraid the answer is going to be "you can't," because the documentation doesn't appear to offer any parameters for setting it, and Google is turning up squat; it's giving me the impression that only setting up sites in a scripted manner is acceptable, and something about configuring Application Pools is just "not done." I can't possibly imagine why not - if you're automating one major part of the process, why can you not automate the other major part?

Anyone who might have some insight on how to do this via PowerShell would be helping me out greatly.

Windows Solutions


Solution 1 - Windows

With the WebAdministration module loaded try this on a pool that you've created:

Set-ItemProperty IIS:\AppPools\<pool_name> managedRuntimeVersion v4.0

Solution 2 - Windows

Import-Module WebAdministration
#Get all web sites
dir IIS:\Sites | ForEach-Object {
  #Go to the app pools root
  cd IIS:\AppPools\
  if (!(Test-Path $_.Name -pathType container))
  {
    #Create the app pool and set .net framework version
    $appPool = New-Item $_.Name
    $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $IISAppPoolDotNetVersion
    #Go to the web sites root
    cd IIS:\Sites\
    $iisApp = Get-Item $_.Name
    $iisApp | Set-ItemProperty -Name "applicationPool" -Value $_.Name
  }
  else {
    $dotNetVersion = (Get-ItemProperty $_.Name managedRuntimeVersion).Value
    if ($dotNetVersion -ne $IISAppPoolDotNetVersion){
        #Get the app pool and set .net framework version
        $appPool = Get-Item $_.Name
        $appPool | Set-ItemProperty -Name "managedRuntimeVersion" -Value $IISAppPoolDotNetVersion       
    } 
  }
} 

You can download detail script from how to set the IIS Application Pool to specify version of the .NET Framework

Solution 3 - Windows

This is what worked for me when you want no runtime specified:

$newAppPool = New-WebAppPool -Name $AppPoolName 
$newAppPool.managedRuntimeVersion = ""
$newAppPool | Set-Item

Solution 4 - Windows

Alternatively, you can set this the on the return value from the New-WebAppPool cmdlet. This approach can be useful if you want to change other properties as well.

For the v4.0 pool it'll look like this:

Import-Module WebAdministration
$appPool = New-WebAppPool -Name Pool1
$appPool.managedRuntimeVersion = "v4.0"
$appPool | Set-Item

To set it to 'No managed code' you can use this:

Import-Module WebAdministration
$appPool = New-WebAppPool -Name Pool2
$appPool.managedRuntimeVersion = ""
$appPool | Set-Item

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMatt DiTrolioView Question on Stackoverflow
Solution 1 - WindowsKeith HillView Answer on Stackoverflow
Solution 2 - Windowsfrank tanView Answer on Stackoverflow
Solution 3 - WindowsVlad SetchinView Answer on Stackoverflow
Solution 4 - WindowsBerndView Answer on Stackoverflow