In this article I want to show you how you can streamline the SharePoint Site creation with PowerShell. You can save time by doing it with PowerShell especially, if you want to create multiple sites as part of a backend process.
Typical backend processes could be:
You have a domain specific process – e.g. as part of each opportunity you want to gather content in a separate SharePoint site. After the opportunity closure you want to make sure that the content is handled as per your organizational guidelines. With the automated approach to create SharePoint sites, you can make sure that the Site is following specified structure – e.g. you keep a naming concept, language settings etc. This is a common use case for many customers I am working with when it comes to SharePoint Site creation with PowerShell.
The SharePoint site creation with PowerShell is a basic task for administrators looking to efficiently manage and scale their SharePoint environments. Especially if the SharePoint environment need to follow a consistent design.
The approach for SharePoint Site creation with PowerShell is pretty straight forward, you just need to authenticate with the admin console, define the parameters and create the SharePoint site.
Table of Contents
Prerequisites for SharePoint Site Creation with PowerShell
In order to create SharePoint sites with PowerShell, following prerequisites need to be fulfilled:
- You need to install the PowerShell module Microsoft.Online.SharePoint.PowerShell. If you haven’t done it yet, feel free to use this comprehensive guide: SharePointOnlinePowerShell: How to connect to SharePoint (sposcripts.com)
- You need to run the PowerShell code with a user accoutn with the SharePoint Administrator or Global Administrator role
Step-by-Step Instructions for SharePoint Site Creation with PowerShell
First of all you need to connect to the SharePoint Admin center in your PowerShell console. Make sure that you adjust the variable SharePointAdminURL, so that you connect to your tenant. The SharePoint Admin URL is specifying the backend address for administrative purposes for your SharePoint tenant. In my case it is “https://m365x04995906-admin.sharepoint.com”, which is derived from the M365 demo tenant prefix.
Import-Module -Name Microsoft.Online.SharePoint.PowerShell
$sharePointAdminURL= "https://m365x04995906-admin.sharepoint.com"
Connect-SPOService -Url $sharePointAdminURL
If you run the code an interactive authentication prompt window will show up, make sure that you use the SharePoint Administrator or Global Administrator for the tenant.
Now you can create the site with following code – again make sure that you update the parameters for your specific use case.
Parameters Explained: SharePoint Site Creation with PowerShell
- Owner (owner)
The owner of the site is defined using the UPN (User Principal Name), which is typically the email address of the user. In this example I use “[email protected]”, which is the global admin for M365 demo tenants. - Storage Quota (storageQuota)
This parameter sets the storage limit for the SharePoint site. Here, I have specified 1000, meaning the site will have 1 GB of storage. Adjust this value according to the needs of your organization, as storage quotas help in managing SharePoint resources efficiently. - Site URL (url)
This is the URL where the new SharePoint site will be accessible. In this case, it’s set to “https://m365x04995906.sharepoint.com/sites/Demo2”. Each site in SharePoint must have a unique URL, so you’ll need to customize this for each new site you create. - Template (template)
SharePoint offers a variety of site templates for different use cases (e.g., team sites, communication sites). In this example, I use “STS#3”, which refers to a Team Site template. You can view all available templates using the Get-SPOWebTemplate cmdlet to find one that suits your needs. - Time Zone (timezoneID)
Setting the correct time zone for your site is important for accurate scheduling and regional settings. The timezoneID parameter uses numeric codes to specify time zones. Here, 4 refers to a specific time zone, and you can find a full list of time zone IDs in the Microsoft documentation. - Site Title (title)
The title is how your site will be identified within SharePoint. In this example, I named the site “Demo2”. This title will be visible in the site header and navigation. - No Wait (noWait)
By setting this parameter to $true, the script does not wait for the site creation process to complete before continuing. This is useful when you need to deploy multiple sites in parallel or run the script in an environment where you don’t need immediate feedback.
$SpoSiteArguments = @{ owner = "[email protected]" #type in the upn of the owner of the site storageQuota = 1000 #specify the quota of the site in MB url= "https://m365x04995906.sharepoint.com/sites/Demo2" #specify the url of the site template = "STS#3" #specify, which template shall be used, run Get-SPOWebTemplate to find available templates timezoneID = 4 #specify the time zone for the site https://learn.microsoft.com/en-us/previous-versions/office/sharepoint-csom/jj171282(v=office.15)#remarks title= "Demo2" #specify the title of the site noWait = $true } New-SPOSite @SpoSiteArguments
Using the New-SPOSite command, you can streamline SharePoint site creation with PowerShell by specifying parameters like owner, storage quota, and site template, so you could make sure that the owner is a specific Entra ID group for e.g. your sales support team.
Create SharePoint Site: Interactive PowerShell code
Below you’ll find the code to create sites interactively. The interactive approach makes sense to get a hands on experience how the code works, before you implement it in production as part of a backend automation. With that you’ll be sure that your infrastructure and modules are all set and settled and that you don’t run into any surprise.
Before you run the code, You need to change the values in the SharePoint Admin URL variable and SPOSiteArguments hashtable.
Param{ $sharePointAdminURL= "https://m365x04995906-admin.sharepoint.com" } Import-Module -Name Microsoft.Online.SharePoint.PowerShell Connect-SPOService -Url $sharePointAdminURL $SpoSiteArguments = @{ owner = "[email protected]" #type in the upn of the owner of the site storageQuota = 1000 #specify the quota of the site in MB url= "https://m365x04995906.sharepoint.com/sites/Demo2" #specify the url of the site template = "STS#3" #specify, which template shall be used, run Get-SPOWebTemplate to find available templates timezoneID = 4 #specify the time zone for the site https://learn.microsoft.com/en-us/previous-versions/office/sharepoint-csom/jj171282(v=office.15)#remarks title= "Demo2" #specify the title of the site noWait = $true } New-SPOSite @SpoSiteArguments
Create SharePoint Site: Non-Interactive PowerShell Code
With the code below, you won’t get any prompt, but you need to make sure that you have stored the credentials locally. This code can be used if you are looking forward to create sites as part of a backend process.
If you have not exported your credentials, you can do it like this:
$credentialPath = "C:\Users\Serka\OneDrive\Desktop\PS\Keys\[email protected]" #Path, where the encrypted credential file will be stored at
$username = "[email protected]" #Usename of the credential object
$credential = Get-Credential($username)
$credential | Export-Clixml -Path $credentialPath
Return $Credential
Param( $sharePointAdminURL= "https://m365x04995906-admin.sharepoint.com", $credentialPath = "C:\Users\Serkar\OneDrive\Desktop\PS\Keys\[email protected]" ) Import-Module -Name Microsoft.Online.SharePoint.PowerShell #region credential import $credential | Import-Clixml -Path $credentialPath Return $Credential #endregion #region create the SPOSite Connect-SPOService -Url $sharePointAdminURL -Credential $credential $SpoSiteArguments = @{ owner = "[email protected]" #type in the upn of the owner of the site storageQuota = 1000 #specify the quota of the site in MB url= "https://m365x04995906.sharepoint.com/sites/Demo2" #specify the url of the site template = "STS#3" #specify, which template shall be used, run Get-SPOWebTemplate to find available templates timezoneID = 4 #specify the time zone for the site https://learn.microsoft.com/en-us/previous-versions/office/sharepoint-csom/jj171282(v=office.15)#remarks title= "Demo2" #specify the title of the site noWait = $true } New-SPOSite @SpoSiteArguments #endregion
With this code, we are now able to create SharePoint sites in bulk in interactive sessions or as part of backend automations. Do you have any questions or ideas for future automation topics? Let me know in the comments below!
References, which you want to check for SharePoint Site creation with Powershell
- To deepen your understanding of SharePoint site creation with PowerShell, check out the official Microsoft documentation.
- You could also handle SharePoint Sites creation with Powershell utilizing Pnp.PowerShell – however it’s limited to some communication sites:
New-PnPSite | PnP PowerShell
Pingback: How to create SharePoint Sites using Graph | SPO Scripts