Is it possible I can use PowerShell command (e,g, New-WebSite) to create a web site and set site's preloadEnabled="true"?
7 Answers
This should do the trick. You can use the get-itemproperty
to verify that it worked. It took me a while to figure out where to find preloadEnabled
within powershell but if you pipe the site path to get-member
, then you can work your way from there.
import-module webadministration
set-itemproperty IIS:\Sites\SiteName -name applicationDefaults.preloadEnabled -value True
-
Works perfectly, important here to remember is: preloadEnabled is case-sensitive. Also, I used ([System.Convert]::ToBoolean("True")) instead of $true. – sebastiaan Nov 04 '13 at 11:24
-
I also needed to write "applicationDefaults" with the correct case to get it working. Otherwise works fine. – Knaģis Dec 11 '13 at 10:13
-
1This is the answer. The trick is adding "applicationDefaults." before "preloadEnabled" – Thomas T Dec 28 '16 at 14:10
-
1this works on server 2008 with iis 7 import-module webadministration set-itemproperty IIS:\Sites\site-name\application-name -name preloadEnabled -value True – Captain Harlock Jul 09 '17 at 13:59
-
2This script does not seem to affect the same property as the one manipulated in IIS Manager. – Thorarin Mar 28 '19 at 11:16
-
Bad approach. It will set `preloadEnabled` to `True` to **all** applications, defined for the website. Even worse, if you run it again `set-itemproperty IIS:\Sites\SiteName -name applicationDefaults.preloadEnabled -value False` - to reverse the changes, it will set `applicationDefaults.preloadEnabled` to `False`, but for all existing applications this parameter will be kept `True` – Sergey Nudnov Mar 09 '21 at 20:07
This is a bit late, but this will help others... This worked for me and was a little less verbose. The key difference is that I removed ApplicationDefaults because I am setting the application, not the defaults here:
Set-ItemProperty IIS:\Sites\<siteName>\<applicationName> -name preloadEnabled -value True
WHERE: 'SiteName' might equal Default Web Site 'ApplicationName' might equal MyApplication

- 428
- 4
- 7
There is in fact a way to do this (assuming you have a single application at / that you want to set it for and you know the name of your site):
[System.Reflection.Assembly]::LoadFrom("C:\windows\system32\inetsrv\Microsoft.Web.Administration.dll")
$serverManager = (New-Object Microsoft.Web.Administration.ServerManager)
$serverManager.Sites["YOUR_SITE_NAME"].Applications["/"].SetAttributeValue("preloadEnabled", $true)
$serverManager.CommitChanges()

- 688
- 5
- 8
-
3This is the only approach that worked for me when setting the preloadEnabled attribute of a specific web site's "default" application (path="/"). Set-ItemProperty does not seem to be able to set the preloadEnabled attribute for the root application. I have tested this successfully on two platforms: Windows 7 SP1 64-bit w/ IIS 7.5 and the Application Initialization module installed as well as Windows Server 2012 R2 64-bit w/ IIS 8.5. Both environments have Windows Management Framework 5.0 RTM (i.e. Powershell 5.0) installed for their respective platforms. – Wade May 16 '16 at 00:10
You can enable preload for the root application of a website like this:
$w = New-Item "IIS:\Sites\AAA" -type site –physicalPath "C:\W" -bindings $binding
$w.Collection[0].preloadEnabled = $true
$w | Set-Item

- 2,572
- 6
- 19
- 33

- 1,238
- 14
- 14
I could offer more granular and deterministic approach:
# Creating the website:
$website = New-WebSite -Name $WebSiteName -PhysicalPath $WebSiteFolder -ApplicationPool $PoolName
# Setting preload enabled:
Set-WebConfigurationProperty `
-PSPath "IIS:\" `
-Filter "/system.applicationHost/sites/site[@name='$WebSiteName']/application[@path='/']" `
-Name "preloadEnabled" `
-Value $True # or $False to turn that off

- 1,327
- 11
- 20
Old question, but I wanted to share my insights.
I needed to do this from an Octopus post-deployment script, where my App was in the Site root. I tried all the suggested solutions here and the one from Robert Moore was the only one working for me.
However, I ended up using this which also did the job:
$Site = Get-Item IIS:\Sites\<site name>
$Site.applicationDefaults.preloadEnabled = $true
$Site | Set-Item -Verbose
Warning: Do not do this if you have any Apps in your Site (see comment from @Sergey Nudnov

- 839
- 1
- 8
- 21
-
Bad approach. It will set `preloadEnabled` to `True` to **all** applications, defined for the website. Even worse, if you run it again `set-itemproperty IIS:\Sites\SiteName -name applicationDefaults.preloadEnabled -value False` - to reverse the changes, it will set `applicationDefaults.preloadEnabled` to `False`, but for all existing applications this parameter will be kept `True` – Sergey Nudnov Mar 09 '21 at 20:08
-
@SergeyNudnov I wasn't aware of that, will add a warning and point to your comment. However in my case, there were no applications in any level below the Site root so this isn't an issue for me. – tobypls Mar 11 '21 at 16:38
I've been looking for this too, but couldn't find anything in WebAdministration to set this option. Presumably the approach would be to call New-ItemProperty on the correct WebApplication. Unfortunately, I was unable to get the "default" application for a given website, or to set this property on it. It kinda seems like the WebAdministration module (which enables cmdlets like New-WebSite) was written with earlier versions of IIS in mind, and certainly before the Application Initialization module.
This is a workaround, which forces the setting of these properties by editing the underlying applicationHost.config file. This is a slightly simplified version of a script we're now using. You'll need to run this script as an administrator.
# Copy applicationHost.config to the temp directory,
# Edit the file using xml parsing,
# copy the file back, updating the original
$file = "applicationhost.config"
$source = Join-Path "$env:windir" "\system32\inetsrv\config\$file"
$temp = Join-Path "$env:temp" "$([Guid]::NewGuid().ToString())"
$tempFile = Join-Path "$temp" "$file"
#update all applications in websites whose name matches this search term
$search = "website name to search for"
#copy applicationHost.config to temp directory for edits
#assignments to $null simply silence output
$null = New-Item -itemType Directory -path $temp
$null = Copy-Item "$source" "$temp"
# Load the config file for edits
[Xml]$xml = Get-Content $tempFile
# find sites matching the $search string, enable preload on all applications therein
$applications = $xml.SelectNodes("//sites/site[contains(@name, `"$search`")]/application")
$applications | % {
$_.SetAttribute("preloadEnabled", "true")
}
#save the updated xml
$xml.Save("$tempFile.warmed")
#overwrite the source with updated xml
Copy-Item "$tempfile.warmed" "$source"
#cleanup temp directory
Remove-Item -recurse $temp

- 120
- 4