Deploy a SharePoint site using automated powershell script

This blog is about deploying a SharePoint solution using powershell script. Usually during the development phase, a developer creates a web application, creates a site collection, then install the WSP solution manually. But the whole process can be automated in order to make deployment simple in testing, UAT or production environments.

We need 3 files in all for the deployment that needs to be placed in same folder:

1. Deploy.bat - A batch file that is used as a trigger to execute the powershell script and can be called from command prompt.
2. Deploy.ps1 - The powershell script file (.ps1).
3. Demo.Project.wsp - The SharePoint solution file.



powershell -Command "& {Set-ExecutionPolicy bypass}" -NoExitpowershell -Command "& {.\Deploy.ps1}" -NoExit



We need some information about the environment and names of objects like site collection, web application, etc. Here we will have all these data stores in configurable variables.

The script will execute following steps including validation checks:

1. Create a web application
2. Create a site collection
3. Add and install WSP solution

You need to set the values of the variables mentioned at starting of the script under headings: Web Application settings, Site Collection Settings and Solution Settings. Once these configuration settings are done, you can run the script


Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

Write-Host Write-Host "Initializing..." -foregroundcolor Green

# Web Application settings$WebAppName = "WebAppTest"$WebAppPort = 100$sp_webapp_url = "http://MACH-I-NE"  #Machine name$WebAppAppPool = "AppPoolTest"$WebAppAppPoolAccount = "domain\user"$WebAppDatabaseName = "WebAppTest_Content_Database"$WebAppDatabaseServer = "MACH-I-NE\SP_SQLDBINSTANCE" 

# Site collection settings$SiteCollectionName = "TestSiteCollection" $SiteCollectionURL =  $sp_webapp_url + ":" + $WebAppPort$SiteCollectionLanguage = 1033 $SiteCollectionOwner = "domain\user"$SiteCollectionTemplate = "BICenterSite#0"    #This is BI site template, you need to select as per your requirement

#Solution settings$SolutionName = "Demo.Project.wsp"

Write-Host Write-Host "-------------------------------------" -foregroundcolor GreenWrite-Host "Creating WebApp using Classic authentication..." -foregroundcolor GreenWrite-Host "-------------------------------------" -foregroundcolor Green

$sp = Get-SPWebApplication | Where {$_.DisplayName -eq $WebAppName} if($sp -eq $null) {   New-SPWebApplication -Name $WebAppName -Port $WebAppPort -URL ($sp_webapp_url) -ApplicationPool $WebAppAppPool -ApplicationPoolAccount (Get-SPManagedAccount $WebAppAppPoolAccount) -DatabaseName $WebAppDatabaseName -DatabaseServer $WebAppDatabaseServer

 # display site info Write-Host  Write-Host "Web App created" -foregroundcolor Green Write-Host "-------------------------------------" -foregroundcolor Green} else {        Write-Host        Write-Host "Web Application already exists, skipping this step" -foregroundcolor yellow}

Write-Host Write-Host "-------------------------------------" -foregroundcolor GreenWrite-Host "Creating site collection..." -foregroundcolor Green

 # Delete any existing site found at target URL 

$targetUrl = Get-SPSite | Where-Object {$_.Url -eq $SiteCollectionURL} if ($targetUrl -ne $null)  {   Write-Host "Site already exist, Deleting existing site : " $SiteCollectionURL -foregroundcolor yellow

   Remove-SPSite -Identity $SiteCollectionURL -Confirm:$false     Write-Host    Write-Host "Existing site collection deleted..." -foregroundcolor yellow }

 # Create new site collection 

New-SPSite -URL $SiteCollectionURL -OwnerAlias $SiteCollectionOwner -Language $SiteCollectionLanguage -Template $SiteCollectionTemplate -Name $SiteCollectionName

Write-Host Write-Host "Site collection created" -foregroundcolor GreenWrite-Host "-------------------------------------" -foregroundcolor Green

Write-Host Write-Host "-------------------------------------" -foregroundcolor GreenWrite-Host "Deploying WSP package..." -foregroundcolor GreenWrite-Host "-------------------------------------" -foregroundcolor Green

 # If solution already exists, Retract, Remove old solution and Add, Deploy new solution

$solution = Get-SPSolution $SolutionName -ErrorAction SilentlyContinue if ($solution -ne $null)  {   Write-Host   Write-Host "Solution already exists. " $solution -foregroundcolor Yellow     $solution | Uninstall-SPSolution -AllWebApplications  -confirm:$false          Write-Host "Retracting solution..." -foregroundcolor Yellow

 # Retracting job is assigned to timer job, so we need to wait for it to complete before removing solution. 

    while ( $solution.JobExists )   {    sleep 3    write-host "."   }   write-host "solution retracted" -foregroundcolor yellow   Write-Host   Write-Host "Removing solution " $solution -foregroundcolor Yellow      $solution | Remove-SPSolution -confirm:$false

   Write-Host "Removed"


   Write-Host    Write-Host "Adding solution " $solution -foregroundcolor Green    Add-SPSolution $SolutionName     Write-Host    Write-Host "Solution Added. Now Installing WSP solution..." -foregroundcolor Green

  $SolutionName | Install-SPSolution –WebApplication $WebAppName -language 0 -GACDeployment

 # Wait for timer job to complete     $solution.JobExists

   while ( $solution.JobExists )   {    sleep 3    write-host "."   }

 write-host "Deployment complete" -foregroundcolor Green Write-Host "-------------------------------------" -foregroundcolor GreenRemove-PsSnapin Microsoft.SharePoint.PowerShell




 Please note that this script does not have all the steps or commands that might be needed for a generic project, but it will give a clear idea about the approach and you can add or remove commands as per your requirements.