Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Trying to write a script here to do change all applications to virtual directories. I'm using Powershell, but my skills there are a bit weak. Am I using the right tool?

Here's what I have so far:

cd $env:SystemRoot\system32\inetsrv\

#Find all applications below RootAppName, convert them to virtual directories

$RootAppName = 'Default Web Site/RootApp'

./appcmd list app  | Where { [Regex]::IsMatch($_, $RootAppName + '/') } | Foreach{
            $FirstIndex = $_.IndexOf('"', 0)
            $SecondIndex = $_.IndexOf('"', $FirstIndex + 1)
            $Appname = $_.Substring($FirstIndex + 1, $SecondIndex - $FirstIndex - 1)

            $PhysicalPath = '' #Can't figure out how to get this
            $VDirPath = $Appname.Replace($RootAppName, '')

            # Need to invoke here appcmd delete app $Appname
            # Need to invoke here appcmd add vdir /$RootAppName /path:$VDirPath /physicalPath:$PhysicalPath

Any ideas? Am I going about this the right way?

share|improve this question
up vote 1 down vote accepted

Urgh! PowerShell 2? Don't shell out to AppCMD.exe.

At the PowerShell prompt do a Import-Module WebAdministration, or just right-click on the PowerShell icon and select "Import System Modules"

Then try Get-WebApplication and Remove-WebApplication.

share|improve this answer
Got it. Thanks. – Shlomo Oct 28 '11 at 4:26
Please see below if you're looking for the finished script. – Shlomo Nov 1 '11 at 16:54

Here's the script for posterity using WebAdministration:

Import-Module WebAdministration

#Find all applications below AppName in site SiteName, convert them to virtual directories

$RootAppName = 'AppName' 
$SiteName = 'SiteName'

$SitePath = 'IIS:\Sites\' + $SiteName 
cd $SitePath

dir | Where { [Regex]::IsMatch($_.Name, $RootAppName + '\\') } | Foreach {
    $AppName = $_.Name
    $PhysicalPath = $_.PhysicalPath
    Remove-WebApplication -Name $AppName -Site $SiteName 
    Write-Host 'Removing application' $AppName 'from site' $SiteName
    New-WebVirtualDirectory -Site $SiteName -Name $AppName -PhysicalPath $PhysicalPath
    Write-Host 'Adding virtual directory' $AppName 'to site' $SiteName 'at path' $PhysicalPath
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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