How to Retrieve SharePoint Online Current Site Title and URL using PowerShell

In this PowerShell SharePoint Online tutorial, we will discuss how to retrieve the current site title and current site URL using PowerShell in SharePoint Online? How to retrieve parent web title and parent web URL using PowerShell in SharePoint Online? And how to retrieve root web title and root web URL using PowerShell in SharePoint Online.

I have a SharePoint online nested subsite like Bhawana ( Root web site ) -> MyTest (First level sub site) -> MyTestSubWeb (Second level sub site).

In the below PowerShell script, I am taking the context of the second level sub site and from there we will retrieve the parent web as well as root web.

Once you run the below PowerShell script, it will ask you for the credentials for the SharePoint online user with which you want to connect to the SharePoint online site.

Provide the password and it will display the above result. You can run, debug and test the PowerShell script using Visual studio code or by using Windows PowerShell ISE.

Get current site title and current site URL using PowerShell in SharePoint Online

Below PowerShell script to retrieve current site title and current site URL using PowerShell in SharePoint Online.

Try{
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$siteUrl = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/MyTest/MyTestSubWeb/"
$username = "bijay@onlysharepoint2013.onmicrosoft.com"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
$rootWeb = $ctx.Web
$ctx.Load($rootWeb)
$ctx.ExecuteQuery()
Write-Host "Site Title: " $rootWeb.Title
Write-Host "Site URL: "$rootWeb.Url

Once you run the PowerShell script, you can see the output like below:

Retrieve current site title and current site url using PowerShell in SharePoint Online
Retrieve current site title and current site url using PowerShell in SharePoint Online

Get parent web title and parent web URL using PowerShell in SharePoint Online

Below is the PowerShell script to retrieve Parent web tile and parent web URL using PowerShell in SharePoint Online.

Try{
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$siteUrl = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/MyTest/MyTestSubWeb/"
$username = "bijay@onlysharepoint2013.onmicrosoft.com"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
$rootWeb = $ctx.Web
$ctx.Load($rootWeb.ParentWeb)
$ctx.ExecuteQuery()
Write-Host "ParentWeb Title: " $rootWeb.ParentWeb.Title
Write-Host "ParentWeb Server Relative URL: " $rootWeb.ParentWeb.ServerRelativeUrl

You can see the output like below:

Retrieve parent web title and parent web url using PowerShell in SharePoint Online
Retrieve parent web title and parent web url using PowerShell in SharePoint Online

Get root web title and root web URL using PowerShell in SharePoint Online

Below is the PowerShell script to retrieve root web title and URL in SharePoint online.

Try{
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'E:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$siteUrl = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/MyTest/MyTestSubWeb/"
$username = "bijay@onlysharepoint2013.onmicrosoft.com"
$password = Read-Host -Prompt "Enter password" -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
$ctx.Credentials = $credentials
$ctx.Load($ctx.Site.RootWeb)
$ctx.ExecuteQuery()
Write-Host "Root Web Title: " $ctx.Site.RootWeb.Title
Write-Host "Root Web URL: " $ctx.Site.RootWeb.Url

You can see the output like below:

Retrieve root web title and root web url using PowerShell in SharePoint Online
Retrieve root web title and root web url using PowerShell in SharePoint Online

Read some PowerShell Tutorials:

In this SharePoint tutorial we learned:

  • How to get current site title and current site URL using PowerShell in SharePoint Online
  • How to get parent web title and parent web URL using PowerShell in SharePoint Online
  • How to get root web title and root web URL using PowerShell in SharePoint Online
>