How can I make sure default.aspx is the first default document for my IIS website?

I have tried:

Add-WebConfiguration //defaultDocument/files "IIS:\sites\Default Web Site\MyWebSite" -atIndex 0 -Value @{value="Default.aspx"}

but if default.aspx is already in the list it complains

Add-WebConfiguration : Filename: 
Error: Cannot add duplicate collection entry of type 'add' with unique key attribute 'value' set to 'Default.aspx'

How can I add it if necessary and move it to the top of the list if it's not already there?


The trick is to remove 'default.aspx' if it is already anywhere in the list:

$filter = "system.webserver/defaultdocument/files"
$site = "IIS:\sites\Default Web Site\MyWebSite"
$file = "default.aspx"

if ((Get-WebConfiguration $filter/* "$site" | where {$_.value -eq $file}).length -eq 1)
   Remove-WebconfigurationProperty $filter "$site" -name collection -AtElement @{value=$file}

Add-WebConfiguration $filter "$site" -atIndex 0 -Value @{value=$file}

We first check for the existence of default.aspx, if found, remove it and then add it back in at the top, just like you already did.

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.