Automate SharePoint Site Creation with PowerShell and Power Automate

Introduction

Creating and managing SharePoint sites quickly and easily is important for any organization. By using PowerShell and Power Automate, you can automate site creation and include useful workflows in your site templates. This guide will show you how to create a custom SharePoint site template that makes setting up new sites simple and consistent.

Key Features of This Approach

  • Power Automate Integration: Automatically run workflows whenever a new site is created.
  • Custom Site Scripts: Use simple scripts to define how your sites look and work.
  • Easy Site Management: Set up themes, time zones, and site logos with just a script.
  • Workflow Automation: Automatically send notifications or track site creation details using workflows.

Benefits

  • Saves Times: Automates repetitive work, freeing up time for your team.
  • Keeps Things Consistent: Makes sure every new site follows the same setup and rules.
  • Scalable: Handles creating lots of sites without manual work.
  • Easy to Customize: You can change templates anytime to match your needs.

Steps to Implement
 

1. Set up a Power Automate Flow

Power Automate will handle workflows triggered during the site creation process.

Steps to create the flow

  1. Go to Power Automate.
  2. Click “New Flow” > “Automated Cloud Flow”.
  3. Name the flow (e.g., “PowerShell through calling a Power Automate”) and click “Skip”.
    Skip
  4. Add the “When an HTTP request is received” trigger.
     HTTP request
  5. Configure the trigger settings.
    1. Under “Who can trigger the flow?” select “Anyone”.
      Trigger
    2. Add the following Request Body JSON Schema.
      {
          "type": "object",
          "properties": {
              "type": {
                  "type": "string"
              },
              "properties": {
                  "type": "object",
                  "properties": {
                      "webUrl": {
                          "type": "object",
                          "properties": {
                              "type": {
                                  "type": "string"
                              }
                          }
                      },
                      "parameters": {
                          "type": "object",
                          "properties": {
                              "type": {
                                  "type": "string"
                              },
                              "properties": {
                                  "type": "object",
                                  "properties": {
                                      "siteName": {
                                          "type": "object",
                                          "properties": {
                                              "type": {
                                                  "type": "string"
                                              }
                                          }
                                      },
                                      "product": {
                                          "type": "object",
                                          "properties": {
                                              "type": {
                                                  "type": "string"
                                              }
                                          }
                                      }
                                  }
                              }
                          }
                      },
                      "webDescription": {
                          "type": "object",
                          "properties": {
                              "type": {
                                  "type": "string"
                              }
                          }
                      },
                      "creatorName": {
                          "type": "object",
                          "properties": {
                              "type": {
                                  "type": "string"
                              }
                          }
                      },
                      "creatorEmail": {
                          "type": "object",
                          "properties": {
                              "type": {
                                  "type": "array",
                                  "items": {
                                      "type": "string"
                                  }
                              }
                          }
                      },
                      "createdTimeUTC": {
                          "type": "object",
                          "properties": {
                              "type": {
                                  "type": "string"
                              }
                          }
                      }
                  }
              }
          }
      }
      
  6. Add any additional actions, such as sending an email creating a list or library, or creating a log.
  7. Save the flow to automatically generate the HTTP POST URL. Copy this URL for use later.
    HTTP POST

2. Write a Site Script

A site script defines the actions SharePoint will execute when the template is applied.

Steps to create the script

  1. Open a text editor. (e.g. Notepad++ or Visual Studio Code).
  2. Copy and customize the following JSON structure.
    $JSONScript = @"
    {
      "$schema": "schema.json",
      "actions": [
        {
          "verb": "applyTheme",
          "themeName": "Custom Theme"
        },
        {
          "verb": "setRegionalSettings",
          "timeZone": 24,
          "sortOrder": 25,
          "hourFormat": "12"
        },
        {
          "verb": "setSiteLogo",
          "url": "/Style Library/logo.jpg"
        },
        {
          "verb": "createSPList",
          "listName": "Work",
          "templateType": 101,
          "subactions": [
            {
              "verb": "setTitle",
              "title": "Work"
            }
          ]
        },
        {
          "verb": "triggerFlow",
          "url": "PASTE_YOUR_FLOW_URL_HERE",
          "name": "PowerShell through calling Flow",
          "parameters": {
            "siteName": "Demo site",
            "product": "SharePoint Online"
          }
        }
      ],
      "bindata": {},
      "version": 1
    }
    "@
  3. Replace PASTE_YOUR_FLOW_URL_HERE with the URL copied from your Power Automate flow.

3. Create a site Template using PowerShell

PowerShell allows you to upload and apply the site script to create a template.

Steps to run the script

  1. Open PowerShell as an administrator.
  2. Connect to SharePoint Online.
    Connect-SPOService –Url https://your-tenant-name-admin.sharepoint.com 
  3. Add the site script.
    $JSONScript = Get-Content -Raw -Path “PathTo\SiteScript.json” 
    $SiteScript = $JSONScript | Add-SPOSiteScript -Title “Demo Site Script”  
  4. Create the site design.
    Add-SPOSiteDesign -Title “Site Template1” -WebTemplate 68 -SiteScripts $SiteScript.ID -Description “Site Template1” 
  5. WebTempalte: User 68 for communication sites or 64 for Team Sites or 1 for Team sites without Group.
    Web Template
  6. Disconnect from SharePoint Online.
    Disconnect-SPOService

4. Create a New Site

Use the SharePoint Admin Center to create a new site with the custom template.

Steps to create the site

  1. Navigate to https://your-tenant-name-admin.sharepoint.com.
  2. Select Active Sites > Create.
    Active site
  3. Choose the site type. (e.g. Communication Site).
    Site type
  4. Under Template, select Your Organization > Your custom template and Click on "Use Template".
    Use template
    Custom
  5. Enter the required site details.
    • Site Name: Provide a site name (e.g. “Demo Communication Site”).
    • Site Description: Add a brief description.
    • Site Owner: Specify the owner.
      Site details
  6. Click Create Site and wait for provisioning to complete.

5. Test the Automation

  1. Open the newly created site.
  2. Verify the following.
    • The applied theme and regional settings.
    • The uploaded site logo.
    • The created SharePoint list.
    • The triggered Power Automate workflow.

6. Manage Templates

To remove an existing template

  1. Reconnect to SharePoint Online in Power Shell.
    Connect-SPOService -Url https://your-tenant-name-admin.sharepoint.com
  2. Get the list of site designs.
    Get-SPOSiteDesign
    Site
  3. Delete the template by ID.
    Remove-SPOSiteDesign -Identity “TemplateID”
    Web Template

Additional Tips for Beginners

  • Test Before Deployment: Always test in a development environment.
  • Error Handling: Enable logging in Power Automate for debugging issues.
  • Template Naming: Use meaningful full names for scripts and templates to avoid confusion.
  • Keep Scripts Modular: Break down complex actions into smaller scripts for easier management.

Advantages

  • Automates complex workflows during site creation.
  • Centralizes configurations, making maintenance easier.
  • Enhances productivity by reducing administrative overhead.

Pros and Cons

Pros Cons
Consistent site creation process. Initial setup requires scripting expertise.
Fully customizable based on organization needs. Debugging errors may require deeper knowledge.
Supports integration with other services. Depends on Power Automate flow reliability.

Additional Tips

  • Test the JSON schema and PowerShell scripts in a development environment before deploying.
  • Use descriptive names for site templates to avoid confusion.
  • Regularly review and update site scripts to align with organizational changes.

Conclusion

Automating SharePoint site creation using PowerShell and Power Automate transforms how you manage site provisioning. This approach enhances efficiency, consistency, and scalability, allowing IT administrators to focus on strategic initiatives. By following this guide, you can create powerful site templates integrated with workflows that meet your organization's needs.