How to Create SharePoint Sub Site using PowerShell

In this Post we are going to look how to create a SharePoint Sub site using PowerShell. I had to do some research but it was not hard to find a solution.

In below Example we will be creating sub site and assigning a Template once it is created. I had a requirement to set a custom Template with basic data, which I have created. It had Many lists and libraries, Nintex Forms, Nintex Workflows Managed Meta Data columns, Content Types etc.

Publishing Nintex Forms through PowerShell I will be discussing in my next post

Once I created the Sub site, I faced a challenge to set the master page because, newly created site is not getting inherited the master page from the parent.
Finally I found the solution from SharePoint Exchange Gentleman called “Evgenii Vilkov” had provided a simple solution to my problem. I have provided a link to same post within comments section in the code snippet.

In below Example I have included logging to a CSV file with the use of Add-Content function in PowerShell

for setting the Master Page, I have refered the code snippet from below post in SharePiont Stack Exchange replyied by "Evgenii Vilkov" in his reply to a Question Reply

$logFilePath ="C:\scripts\logs" # Path to save the log file

function Create-SubSite
    param($spSite, $subSiteURL, $siteTemplate, $siteName )

    #$spSite => Site collection
    #subSiteURL => URL of the site which we are trying to assign the Master Page
    #$steTemplate => Template of the site I have used a custom template.
    #$siteName => Name of the site


        #Initiating the New Site
        $newweb = New-SPWeb -Url $subSiteURL -Name $siteName;
        Add-Content -Path $logFilePath -value " $($siteName) | $($siteURL) | new site creation Succussfull | success";
        #assign Custom Template
        Add-Content -Path $logFilePath -value " $($siteName) | $($siteURL) | Template Assignment Succussfull | success";

        #Set Master Page for the site

        Write-Host "Master Page Assignment in progress..."

        $newweb.MasterUrl = $spSite.RootWeb.ServerRelativeUrl +  $systemMasterPage
        $newweb.CustomMasterUrl = $site.RootWeb.ServerRelativeUrl + $systemMasterPage;

        Write-Host $newweb.MasterUrl;               
        Write-Host "Master Page Assignment Completed."

        Add-Content -Path $logFilePath -value " $($siteName) | $($siteURL) | Master Page Assignment Succussfull | success";

        $ErrorMessage ='';
        $ErrorMessage = $_.Exception.Message
        Write-Host $ErrorMessage;
        Add-Content -Path $logFilePath -value " $($siteName) | $($siteURL) | Create Sub Site Failed  | $($ErrorMessage)";       


Once I implement this, my next challenge was to publish Nintex Forms on newly created site. for some reason all my Nintex forms were successfully included in the new site. But I had to republish each and every form manually.

I will discuss about how I was able to solve the problem in a future post.

I hope this might help any one like me who is looking for a code snippets to automate.

Leave a Reply

Your email address will not be published. Required fields are marked *