How to retrieve web and list server relative URL using PowerShell in SharePoint Online

Here we will discuss how we can retrieve web server relative URL as well as we will see how we can retrieve list server-relative URL using PowerShell in SharePoint online.

You can run, debug, and test the PowerShell script using Windows PowerShell ISE or by using visual studio code also.

Retrieve list server-relative URL using PowerShell SharePoint Online

Here I have a list name as “Logging” and here we wanted to retrieve the list server relative url.

Try{
Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$SiteURL = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"
$ListName="Logging"
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$securePassword=ConvertTo-SecureString "**********" -AsPlainText -Force
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("bijay@onlysharepoint2013.onmicrosoft.com", $securePassword)
$web = $Context.Web
$List = $web.get_lists().getByTitle($ListName)
$Context.Load($List.RootFolder)
$Context.ExecuteQuery()
$listUrl = $list.RootFolder.ServerRelativeUrl
write-host $listUrl
Retrieve web server relative url and list server relative url using PowerShell SharePoint Online
Retrieve web server relative url and list server relative url using PowerShell SharePoint Online

Retrieve web server relative URL using PowerShell SharePoint Online

Below is the PowerShell script which will be helpful in retrieving the web server relative URL of a SharePoint online site.

Try{
Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.dll'
Add-Type -Path 'C:\Users\Bijaya.Sahoo\Desktop\Microsoft.SharePoint.Client.Runtime.dll'
}
catch {
}
$SiteURL = "https://onlysharepoint2013.sharepoint.com/sites/bhawana"
$UserName="bijay@onlysharepoint2013.onmicrosoft.com"
$Password="*********"
Function Get-SPOCredentials([string]$UserName,[string]$Password)
{
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
return New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
}
$context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$context.Credentials = Get-SPOCredentials -UserName $UserName -Password $Password
$web = $context.Web
$context.Load($web)
$context.ExecuteQuery()
$myweb=$web.ServerRelativeURL
Write-Host $myweb

Read some PowerShell Tutorials:

I hope this PowerShell script will be helpful to retrieve web and list server relative URL using PowerShell in SharePoint Online.

>