Create SharePoint hub sites by using PowerShell

In this article

  1. Connect to SPO
  1. Create a new hub site
  1. Set properties and permissions on the hub site
  1. Create and associate a new site
  1. Confirm the hub site is working
  1. See also

If you’re a global or SharePoint admin in Office 365, you can evolve any existing site to a hub site by using Microsoft PowerShell. In this example, you’ll learn how to create a SharePoint hub site and to associate another site with it. In this scenario, you are setting up sites for the Contoso marketing department:

  • You will create a hub site that all other marketing sites will be associated with.
  • You will then specify settings and permissions for the hub site.
  • Finally, you will create a second site and associate it with the hub site.

Note:

To work with this example in SharePoint Online, we recommend that you use a developer tenant and not your production tenant. All of the following steps use a fictional tenant named “Contoso” that you can replace with your tenant name.

Connect to SPO

First, you need to connect to SharePoint Online by using PowerShell. The commands use both the SharePoint Online Management Shell (-SPO) and the SharePoint PnP PowerShell Online module (-PnP).

  1. Launch Windows PowerShell.
  1. Run the Connect-SPOService cmdlet to connect to SharePoint Online. Log in with your global or SharePoint admin credentials:PowerShellCopy Connect-SPOService -Url "https://<your tenant name>-admin.sharepoint.com"

Note

In the remainder of this exercise, contoso will be used as the tenant name. Continue to use your own tenant name, effectively replacing contoso.

Create a new hub site

Next, design the marketing site that will occupy the hub site that other sites can associate with. The goal is that any sites that are marketing-oriented will integrate with the hub site. This relays common navigation and branding across the associated sites, allows team members to search across all the sites associated with the single hub site, and optimises other hub site features.

  1. Connect to the SharePoint Online Admin centre using:

Connect-PnPOnline -Url https://contoso-admin.sharepoint.com -UseWebLogin

  1. Produce the site to be created as a hub site using the New-PnPSite cmdlet:

Connect-PnPOnline -Url https://contoso-admin.sharepoint.com -UseWebLogin

New-PnPSite -Type TeamSite -Title “Contoso marketing division” -Alias “marketing” -Description “Main site for collaboration for marketing teams at Contoso”

The cmdlet attains the URL of the new site similar to the following:Copyhttps://contoso.sharepoint.com/sites/marketing

  1. Register the new marketing site as a hub site by using the Register-SPOHubSite cmdlet:

Register-PnPHubSite -Site https://contoso.sharepoint.com/sites/marketing

Set properties and permissions on the hub site

The hub site doesn’t include a logo or description yet. We also want to restrict it to ensure that just one person can set changes to the hub site.

Set properties

  1. Upload a logo image for the site by going to https://contoso.sharepoint.com/sites/marketing/SiteAssets and uploading any image you like. Record the image file name.

  1. Use the Set-SPOHubSite cmdlet to set the logo and description. In place of mylogo.jpg, specify the name of the image that you uploaded:PowerShellCopy Set-SPOHubSite -Identity https://contoso.sharepoint.com/sites/marketing -LogoUrl https://contoso.sharepoint.com/marketing/SiteAssets/mylogo.jpg -Description "Main hub site for collaboration on marketing activities across Contoso"

Set permissions

Now we will restrict access so that only the user nestorw@contoso can apply changes to the hub site associations.

  • Run the Grant-SPOHubSiteRights cmdlet to offer a user rights to the marketing hub site. We’ll use nestorw@contoso in this example, but you can use any valid user on your tenant (you can confirm numerous multiple users by separating them with a comma):

Grant-PnPHubSiteRights -Identity https://contoso.sharepoint.com/sites/marketing -Principals “nestorw@contoso” -Rights Join

Create and associate a new site

The final step is to formulate the desired site to associate with the hub. You can repeat these steps for as many sites as you want to join to the hub.

  1. Provision the site by using the New-PnPSite cmdlet:

New-PnPSite -Type TeamSite -Title “Online advertising team” -Alias “online-advertising” -Description “For collaboration on online advertising resources”

  1. Associate this site with the hub site by using the Add-SPOHubSiteAssociation cmdlet:

Add-PnPHubSiteAssociation
-Site https://contoso.sharepoint.com/sites/online-advertising
-HubSite https://contoso.sharepoint.com/sites/marketing

Confirm the hub site is working

To confirm, you can either:

The hub site navigation displays at the top of the site. If you go to the https://contoso.sharepoint.com/sites/online-advertising site, it presents the same hub site navigation at the top.

See also

Leave a Reply

%d bloggers like this: