Create and Delete Web App, Site, and Web using PowerShell

SharePoint 2010 retains the ambiguous naming of the various containers so that anyone new to the technology will end up getting confused. As a result, I feel compelled to explain, as best I can, everytime the subject comes up, the relationships using the appropriate terms: Web Apps, Sites and Webs. At the same time, we can hook them in our head to the SMS commands that we use to work with them.

Web Apps are an IIS entity. Consequently, while web apps can share ApplicationPools, everything in a single web app shares a single Application Pool. Also, while a single web app can support any number of site collections, it can only have one host header. This means that one site collection can own the host header url : http://<host header> while all the other will have a url: http://<host header>/SiteCollectionTopLevelSiteName.

In SMS we use new-SPWebApplication and remove-SPWebApplication to create and destroy web apps.

From the SharePoint administrators’ point of view, a “Site” is really a “Site Collection” which is a single top-level site and zero or more sub-sites. Interactively, Site Collections are created in Central Admin and are assigned to a specfic web app and have their own content database. If there’s already a Site Collection using the web app’s host header, Central Admin will make you give it a URL under that host header.

In SMS we use new-SPSite and remove-SPSite to create and delete a site collection.

Finally, a Web, aka Team Site, is a single SharePoint site. It uses a site template and can support any number of sites below it. Webs are either a top level site or a sub-site.

In SMS, we use new-SPWeb and remove-SPweb to create and delete webs.  

So, after you install SharePoint, if you can open Central Admin and if you can open the SharePoint Management Shell, you can create and delete Web Apps, Sites and Web at will. While it’s easy enough using Central Admin, I was thinking the script that does all of this might be interesting.

I came up with this:

function Pause ($Message="Press any key to continue...")
{
Write-Host -NoNewLine $Message
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Write-Host ""
}

Set-ExecutionPolicy RemoteSigned

New-SPWebApplication -Name MyHostHeader -Port 80
   -HostHeader MyHostHeader -URL http://MyHostHeader
   -AllowAnonymousAccess -ApplicationPool MyHostHeaderAppPool
   -ApplicationPoolAccount (Get-SPManagedAccount MyServer\SVC_SPAppPool03)
ECHO "Web Application Created"
ECHO ""
PAUSE

New-SPSite -Url http://MyHostHeader -OwnerAlias MyServer\Administrator
   -Name MyHostHeader -Template "STS#0"
ECHO "Site Collection Created"
ECHO ""
PAUSE

New-SPWeb -Url http://MyHostHeader/TeamSite -Template "STS#0"
ECHO "Team Sub-Site Created"
ECHO ""
PAUSE

Remove-SPWeb http://MyHostHeader/TeamSite
ECHO "Team Sub-Site Deleted"
ECHO ""
PAUSE

Remove-SPSite -Identity http://MyHostHeader
Echo "Site Collection Deleted"
ECHO ""
PAUSE

Remove-SPWebApplication -Identity http://MyHostHeader
   -DeleteIISSite -RemoveContentDatabase
Echo "Web Application Deleted"
ECHO ""
PAUSE

To see it work, copy it to Notepad and delete the line breaks I added to help with formatting here on the page.  Save it as MyScript.ps1. Open your SharePoint Management Shell and run it by using the full path and filename. If you navigate to the folder where you saved it, you have to call it with:

 .\MyScript.ps1

This will create a web app, site and web and then delete the web, the site and the web app, each time, pausing so you can browse out an see it. I got the PAUSE function from the PowerShell Team blog here.

hth

-robot


Tags:

 
 
 

Comments are closed.