Wednesday, November 10, 2010

Create Web Application, Site Collection and Sub Sites through PowerShell in SharePoint 2010

        This is time to manage SharePoint Server Farms using PowerShell and to say goodbye to STSADM. SharePoint Administrators are learning PowerShell and are writing scripts for their environments. Here is a script for Creating Web Application, Site Collection and multiple sub sites in one go.

1. Create a .ps1 file and add reference for PowerShell and SharePoint Assemblies.
Add-PsSnapin Microsoft.SharePoint.PowerShell
[void][System.Reflection.Assembly]::LoadWithPartialName(“Microsoft.SharePoint”)
2. Set variables and create Web Application.
#Create Web Application 
Write-Output " "
Write-Output "Creating Web Application for Intranet Portal."
$AppName = "Intranet Portall"
$AppPool = "IntranetPortalApplicationPool"
$UserAccount = "winsmarts\sp_admin"
$Port = 80
$DBServer = "SPDEV\SQL_SERVER"
$DBName = "IntranetPortalDatabase_80"
New-SPWebApplication -Name $AppName -Port $Port  -ApplicationPool $AppPool
-ApplicationPoolAccount (Get-SPManagedAccount $UserAccount) -databaseserver $DBServer -databasename $DBName 
Write-Output " "
Write-Output "Web Application Created Successfully."
3. Create Site Collection.
Write-Output " "
Write-Output "Creating Top Site Collection"
# Set variables  
$SiteCollectionName = "Test Portal" 
$SiteCollectionURL = "http://SPDEV:80" 
$SiteCollectionTemplate = "STS#0" 
$SiteCollectionLanguage = 1033  
$SiteCollectionOwner = "winsmarts\Administrator" 
# Create a new Sharepoint Site Collection  
New-SPSite -URL $SiteCollectionURL -OwnerAlias $SiteCollectionOwner -Language $SiteCollectionLanguage
-Template $SiteCollectionTemplate -Name $SiteCollectionName 
Write-Output " "
Write-Output "Top Site Collection created successfully."
4. Create sub sites.
#Creating Sub Sites in top site collection.
Write-Output " "
Write-Output "Creating Sub Sites"
$SubSites = @("Central Services", "Knowledge Base", "Service Center", "IT", "HR", "Finance")
for($i=0 ; $i -lt $SubSites.count ; $i++)
{
clear-variable -name siteurl
$SiteUrl = $SiteCollectionURL += "/" 
$SiteUrl = $SiteUrl += $SubSites[$i]
Write-Output " "
#Write-Output "Creating Site for " += $SubSites[$i]
Write-Output " "
New-SPWeb $SiteUrl -Template $SiteCollectionTemplate -Name $SubSites[$i]  -UseParentTopNav
-Language $SiteCollectionLanguage
Write-Output " "
#Write-Output "Site Created for " += $SubSites[$i]
Write-Output " "
}
 

No comments:

Post a Comment