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).
SharePoint Online tutorial contents:
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.
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:
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:
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:
Read some PowerShell Tutorials:
- PowerShell find files modified in last 24 hours and PowerShell get last modified time of files in a folder
- PowerShell find files modified in last N days
- Upload bulk documents to SharePoint 2016 document library using PowerShell
- Bulk SharePoint Online Site Collection Creation using PowerShell
- Get SharePoint Online Site and List Templates using PowerShell
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
Hello Everyone!! I am Bhawana a SharePoint MVP and having about 10+ years of SharePoint experience as well as in .Net technologies. I have worked in all the versions of SharePoint from wss to Office 365. I have good exposure in Customization and Migration using Nintex, Metalogix tools. Now exploring more in SharePoint 2016 🙂 Hope here I can contribute and share my knowledge to the fullest. As I believe “There is no wealth like knowledge and no poverty like ignorance”