7

I want to create a web application in IIS that does not live at the root of the IIS site.

i.e. MySite/beta/WebApplication.

This is my starting point:

New-WebApplication "WebApplication" -Site "MySite" -ApplicationPool "MyAppPool" -PhysicalPath "C:\Sites\MySite\beta\WebApplication"

That creates me the physical structure I want C:\Sites\MySite\beta\WebApplication , but makes IIS look like this:

MySite (IIS Web Site)

WebApplication (IIS WebApplication)

beta (Folder)

WebApplication (Folder)

Is there a way this can be done via powershell? I do not really want beta to be a web application, just a folder.

1
  • I have the exact requirement like this, the user wants v104, v105, this sort of versions like you beta. Do you have a solution now?
    – Jirong Hu
    Dec 5, 2017 at 2:25

3 Answers 3

13

I know this post is a little older but here is a powershell script I wrote that converts an existing folder to a web application in IIS or if it doesn't exist creates a new folder and web app. It also creates the app pool for it as well. It receives an array of app names so you can create more than one web application. This was my first powershell script so if you have any suggestions feel free to comment.

#Receives an array of appnames and creates the app pools and web applications or converts the folder to an application

Param([parameter(Mandatory=$true)][string[]]$appNames)
$useDefaultPhysicalPath = Read-Host "Would you like to use the default physical path? (C:\inetpub\wwwroot\)";
Import-Module WebAdministration;

$physicalPath = "C:\inetpub\wwwroot\";
if(!($useDefaultPhysicalPath.ToString().ToLower() -eq "yes" -or $useDefaultPhysicalPath.ToString().ToLower() -eq "y"))
{
   $physicalPath = Read-Host "Please enter the physical path you would like to use with a trailing \ (do not include the app name)";
}


$appPath = "IIS:\Sites\Default Web Site\";

foreach($appName in $appNames)
{

if((Test-Path IIS:\AppPools\$appName) -eq 0)
{

    New-WebAppPool -Name $appName -Force;
}

if((Test-Path $appPath$appName) -eq 0 -and (Get-WebApplication -Name $appName) -eq $null)
{  
    New-Item -ItemType directory -Path $physicalPath$appName; 
    New-WebApplication -Name $appName -ApplicationPool $appName -Site "Default Web Site" -PhysicalPath $physicalPath$appName;
}
elseif((Get-WebApplication -Name $appName) -eq $null -and (Test-Path $appPath$appName) -eq $true)
{
    ConvertTo-WebApplication -ApplicationPool $appName $appPath$appName;
}
else
{
    echo "$appName already exists";
}
}
1
  • There is an extra beta (Folder).
    – Jirong Hu
    Dec 5, 2017 at 2:24
5

Since some people use the term folder and Virtual Directory, I thought it would be worth posting how you create a New Web Application inside a Folder / Virtual Directory. Which looks like this in IIS Manager. enter image description here

You simply need to use the Path as the site name.

Import-Module WebAdministration
$SiteName = "Act.Web.API" # IIS Site Name 
New-WebVirtualDirectory -Site $SiteName -Name APFW-api -PhysicalPath C:\inetpub\wwwroot
New-WebApplication -Name Act.Web.API -Site $SiteName\APFW-api -PhysicalPath "C:\Program Files (x86)\ACT\APFW-api" -ApplicationPool $SiteAppPool 

$SiteName\APFW-api equals "Act.Web.API\APFW-api" at run time so the highlighted application is created.

2
  • This looks like what I'm after. Thanks! Nov 27, 2018 at 10:33
  • It fails when physical path is some network path
    – fmsrv
    Jan 2, 2021 at 13:34
5

Since you are using the same physical file repository that is used by the "MySite" collection, it will create the "beta" folder. If you place this new web application in its own path (i.e., "C:\Sites\WebApps\WebApplication") you will get the desired results. The code below worked for me.

New-WebApplication "TestingViaPosh" -Site "Default Web Site" -ApplicationPool "DefaultAppPool" -
PhysicalPath "C:\Users\MyUserId\Documents\TestWebApp"

EDIT: To create a web application in a folder underneath the root of a website, you need to first create the folder in the site you desire (i.e., "C:\Sites\MySite\Beta"). Then the Powershell command will look like this:

New-WebApplication "TestingViaPosh" -Site "Default Web Site\Beta" -ApplicationPool "DefaultAppPool" -PhysicalPath "C:\Users\MyUserId\Documents\TestWebApp"
4
  • But where did the app get created? Directly under Default Web Site or inside the beta folder? I know where it ends up physically, I'm concerned with what it looks like in IIS Manager. Jan 17, 2014 at 13:49
  • For me, the new web application was created under the Default Web Site. There is no beta folder in this instance. Just separate where the files are located for your "MySite" and your new Web Application.
    – websch01ar
    Jan 17, 2014 at 14:10
  • Unfortunately, that's the issue I'm trying to resolve. I don't want the web application to be located at the root of the site, I want it to appear in the folder. Jan 17, 2014 at 14:25
  • That was completely unclear from the OP. Let me tinker with this and find a resolution.
    – websch01ar
    Jan 17, 2014 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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