Office 365 – PowerShell script to create hub site collection
Hi All,
Today we will discuss creating hub sites using PowerShell script.
I try to put detailed comments, tried to be self explanatory.
Following is the complete PowerShell script.
<#
.SYNOPSIS
Ensures that the Hub site is available and if not then creates new Hub site
.PARAMETER CredentialFilePath
Office 365 system account credential file path having two lines in following format
UserName
Password
.PARAMETER TenantAdminURL
Office 365 tenant admin site URL
.PARAMETER HubSiteType
TeamSite OR CommunicationSite. Here we are specifying default type as "TeamSite".
.PARAMETER HubSiteTitle
Title of Hub Site
.PARAMETER HubSiteAlias
Alias of Hub Site
.PARAMETER HubSiteDescription
Description of Hub Site
#>
param
(
[parameter(Mandatory=$true)][string]$CredentialFilePath,
[parameter(Mandatory=$true)][string]$TenantAdminURL,
[parameter(Mandatory=$true)][ValidateSet("TeamSite", "CommunicationSite")][string]$HubSiteType="TeamSite",
[parameter(Mandatory=$true)][string]$HubSiteTitle,
[parameter(Mandatory=$true)][string]$HubSiteAlias,
[parameter(Mandatory=$true)][string]$HubSiteDescription
)
if(-not(Get-PSSnapin | Where { $_.Name -eq "Microsoft.SharePoint.PowerShell"}))
{
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
#Get the user credential file path and getting user from it
$user = Get-Content $CredentialFilePath | Select-Object -First 1
#Getting password
$password = Get-Content $CredentialFilePath | Select-Object -First 1 -Skip 1
$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
#Connect to Office 365
$spoManagementCred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $user, $securePassword
Connect-SPOService -Url $TenantAdminURL -Credential $spoManagementCred
Connect-PnPOnline -Credentials $spoManagementCred -Url $TenantAdminURL
$URL = New-PnPSite -Type $HubSiteType -title $HubSiteTitle -alias $HubSiteAlias -Description $HubSiteDescription
Register-SPOHubSite -Site $URL -Principals $null
References:
PowerShell cmdlets for SharePoint hub sites
Thanks for Reading 🙂
Keep reading, share your thoughts, experiences. Feel free to contact us to discuss more. If you have any suggestion / feedback / doubt, you are most welcome.
Stay tuned on Knowledge-Junction, will come up with more such articles
