Search code examples
powershelliisvirtual-directory

Remove-WebVirtualDirectory...no force/silent option?


Maybe I'm just blind, but I'm trying to run the following command and have tried -recurse, -force, -confirm:$false and nothing is working. Keeps telling me that there are children and asking if I want to remove them. I'm going to be using this script on multiple remote servers so can't have any prompts.

Is there no way to remove a virtual directory silently? If the code below won't work, anyone else have a tip on how I can cleanup IIS to not have this virtual directory under my default web site?

I took the suggested edits, but am still not having much luck with any of the methods I've used to suppress messages in other situations. I'm starting to think one does not exist for this situation which is a terrible shame for those of us who manage dozens of IIS servers, etc.

if (Test-Path "IIS:\Sites\Default Web Site\LIT") {
    Remove-WebVirtualDirectory -Site "Default Web Site" -Application "\" -Name LIT
    Write-Host "  Virtual directory 'LIT' deleted..." -foreground "green"
} else {
    Write-Host "  Unable to delete 'LIT' virtual directory because it was not found..." -foreground "red"
}

Solution

  • If you are using newer versions of Powershell you can use Remove_Item.

    Remove-Item "IIS:\Sites\Default Web Site\LIT" -Force -Recurse
    

    I tested this using PowerShell version 4.0.