Monday, August 18, 2014

Create SharePoint site in bulk using powershell script

Create SharePoint site, subsites in bulk using powershell script and also enable required feature on it

Create below function and save it as Sharedfunction.ps1

function CreateWeb
(
$url,
$name
)
{

$web= New-SPWeb -url $url -name $name -template Templatecode -UseParentTopNav
Write-Host "$web created succesfully for site $url"

Enable-SPFeature -identity "GUID" -Url $url
Enable-SPFeature -identity "GUID" -Url $url
Enable-SPFeature -identity "GUID" -Url $url
Enable-SPFeature -identity "GUID" -Url $url

 Write-Host "Feature activated on $web succesfully"


}

Copy below code and save it as ps1

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

. ".\SharedFunctions.ps1"

Start-SPAssignment -Global

StartTranscript "Create Web"

try {

createWeb http://sitename/test/site1 "Sitename";
createWeb http://sitename/test/site2 "Sitename";

}
catch {
    write-host "Caught an exception:" -ForegroundColor Red
    write-host "Exception Type: $($_.Exception.GetType().FullName)" -ForegroundColor Red
    write-host "Exception Message: $($_.Exception.Message)" -ForegroundColor Red
    exit 1
}

finally {
    Stop-SPAssignment -Global
    StopTranscript

}

No comments:

Post a Comment