@@ -29,15 +29,29 @@ $newListName = "Logo Universe"
2929# Connect to SharePoint online site
3030Connect-PnPOnline -Url $siteUrl -Interactive
3131
32- # Get the SharePoint list
33- $list = Get-PnPList -Identity $oldListName
34-
35- # Move SharePoint list to the new URL
36- $list.Rootfolder.MoveTo($newListUrl)
37- Invoke-PnPQuery
38-
39- # Rename List
40- Set-PnPList -Identity $oldListName -Title $newListName
32+ try {
33+ # Get the list object
34+ $list = Get-PnPList -Identity $oldListName -ErrorAction Stop
35+
36+ # Check if the target URL already exists
37+ $existingFolder = Get-PnPFolder -Url $newListUrl -ErrorAction SilentlyContinue
38+ if ($existingFolder) {
39+ Write-Host "Error: A list or folder already exists at '$newListUrl'. Aborting." -ForegroundColor Red
40+ return
41+ }
42+
43+ # Move the list's root folder to the new URL
44+ $list.RootFolder.MoveTo($newListUrl)
45+ Invoke-PnPQuery
46+ Write-Host "List URL successfully changed to '$newListUrl'." -ForegroundColor Green
47+
48+ # Rename the list display name
49+ Set-PnPList -Identity $list -Title $newListName
50+ Write-Host "List display name successfully changed to '$newListName'." -ForegroundColor Green
51+
52+ } catch {
53+ Write-Host "An error occurred: $_" -ForegroundColor Red
54+ }
4155
4256```
4357
@@ -50,6 +64,7 @@ Set-PnPList -Identity $oldListName -Title $newListName
5064| Author(s) |
5165| -----------|
5266| [ Ganesh Sanap] ( https://ganeshsanapblogs.wordpress.com/about ) |
67+ | ojopiyo |
5368
5469[ !INCLUDE [ DISCLAIMER] ( ../../docfx/includes/DISCLAIMER.md )]
5570<img src =" https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-change-list-url " aria-hidden =" true " />
0 commit comments