Office 365 – PowerShell script to create Hub site

Hi All,

Today, I got an opportunity to write PowerShell script to create hub site. So thought to be share with all of us.

I Put detailed comments, tried to be self explanatory.

<#
.SYNOPSIS
 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

 .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

#Creating site
$URL = New-PnPSite -Type $HubSiteType -title $HubSiteTitle -alias $HubSiteAlias -Description $HubSiteDescription

#register site as hub site
Register-SPOHubSite -Site $URL -Principals $null

References:
Connect-PnPOnline
New-PnPSite
Register-SPOHubSite

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.

Prasham Sabadra

LIFE IS VERY BEAUTIFUL. ENJOY THE WHOLE JOURNEY :) Founder of Microsoft 365 Junction, Speaker, Author, Learner, Developer, Passionate Techie. Certified Professional Workshop Facilitator / Public Speaker. Believe in knowledge sharing. Around 20+ years of total IT experience and 17+ years of experience in SharePoint and Microsoft 365 services Please feel free me to contact for any SharePoint / Microsoft 365 queries. I am also very much interested in behavioral (life changing) sessions like motivational speeches, Success, Goal Setting, About Life, How to live Life etc. My book - Microsoft 365 Power Shell hand book for Administrators and Beginners and 100 Power Shell Interview Questions - https://www.amazon.in/Microsoft-Administrators-Beginners-Interview-Questions/dp/9394901639/ref=tmm_pap_swatch_0?_encoding=UTF8&qid=1679029081&sr=8-11

You may also like...

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Microsoft 365

Subscribe now to keep reading and get access to the full archive.

Continue reading