SharePoint 2007: PowerShell Script to delete the unused empty lists before move to the Cloud

Steel Contributor

 

 

The case of migration from OnPremise to the cloud requires to cleanup the original content to save time, resources, perfomances, ...

 

That script can be executed at a SP2007 Web Application level to check at all the SPWebs the empty lists created with the basic template "Teamsite" without any change since the creation and totaly empty.

 

[int]$LimitMaxNumber = 5000 
[int]$NumberMonthsOlder = -18 #(around 18 months before)

[boolean]$RemoveBasicEmptyLists = $true

[array]$ExceptionListnames = "List Template Gallery", "Master Page Gallery", "Site Template Gallery", "Web Part Gallery", "User Information List", "Settings", "Galería de plantillas de sitios", "Galería de plantillas de listas", "Galerie de modèles de sites", "Galerie de modèles de listes", "Galeria de Modelos de Site", "Galeria de Modelos de Lista"

[array]$BasicListToDelete = "Shared Documents", "Announcements", "Calendar", "Links", "Tasks", "Team Discussion", "Events", "General Discussion", "Contacts", "Discusión de grupo", "Tareas", "Documentos compartidos", "Calendario", "Eventos", "Contactos", "Discusión general", "Vínculos", "Anuncios", "Avisos", "Tarefas", "Documentos Compartilhados", "Contatos", "Discussão Geral", "Calendário", "Discussão em Equipe", "Eventos", "Discussion générale", "Tâches", "Événements", "Documents partagés", "Annonces", "Contacts", "Liens", "Calendrier", "Discussion d'équipe", "Извещения", "Контакты", "События", "Общие обсуждения", "Ссылки", "Ankündigungen", "Teamdiskussion", "Hyperlinks", "Freigegebene Dokumente", "Aufgaben", "Ankündigungen", "Kalender", "Documenti condivisi","Annunci", "Attività", "Calendario", "Collegamenti", "Discussione team", "Dokumenty udostępnione", "Anonsy", "Kalendarz", "Łącza", "Zadania", "Dyskusja zespołu", "共享文件", "宣告", "工作", "行事曆", "連結", "小組討論", "共有ドキュメント", "お知らせ", "タスク", "リンク", "予定表", "チーム ディスカッション", "공유 문서", "공지 사항", "링크", "일정", "작업", "팀 토론"

[array]$AnnouncementListNames = "Announcements", "Anuncios", "Annonces", "Ankündigungen", "Annunci", "Anonsy", "Извещения", "Avisos", "宣告", "お知らせ", "공지 사항"

[array]$AnnouncementDefaultItemValue = "Get Started with Windows SharePoint Services!", "¡Introducción a Windows SharePoint Services!", "Introducción a Windows SharePoint Services", "Guide de mise en route de Windows SharePoint Services", "Erste Schritte mit Windows SharePoint Services", "Introduzione a Windows SharePoint Services", "Rozpocznij pracę z programem Windows SharePoint Services!", "Начало работы с Windows SharePoint Services", "Bem-vindo ao Windows SharePoint Services!", "開始使用 Windows SharePoint Services!", "Windows SharePoint Services について", "Windows SharePoint Services를 시작합니다!"

function Check-Lists([string]$webURL) 
{ 
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null 
    $mysite = new-object Microsoft.SharePoint.SPSite($webURL) 
    $myweb = $mysite.openweb() 
    Write-Host "URL", $webURL 
    #Write-Host "web", $myweb.Name 
    Write-Host "Number of lists", $myweb.lists.count 
    $ListsToDeleteFoSPWeb = New-Object System.Collections.ArrayList 
    
    $LimitDate = (get-date).Addmonths($NumberMonthsOlder) 
    Write-Host " >> Limit date for the lists -[", $LimitDate, "]"

    foreach($MyList in $myweb.lists) 
    { 
        if($ExceptionListnames -notcontains $MyList.Title) 
        { 
            if(($MyList.ItemCount -lt 1) -and ($MyList.LastItemModifiedDate -lt $LimitDate)) 
            { 
                Write-Host "     ------------------------------------ " 
                Write-Host "      List Name:", $MyList.Title, "- GUID:", $MyList.ID -foregroundcolor green 
                Write-Host "      >>Items Count:", $MyList.ItemCount -foregroundcolor green 
                Write-Host "      >>Last modified Date:", $MyList.LastItemModifiedDate -foregroundcolor green 
                if($BasicListToDelete -contains $MyList.Title) 
                { 
                    Write-Host "       >>>>> List to delete: ", $MyList.Title, " <<<<<" -foregroundcolor green 
                    $ListsToDeleteFoSPWeb += $MyList.ID 
                } 
            } 
            else 
            { 
                if(($AnnouncementListNames -contains $MyList.Title) -and ($MyList.ItemCount -eq 1) -and ($MyList.LastItemModifiedDate -lt $LimitDate))
                { 
                    if($AnnouncementDefaultItemValue -contains $MyList.Items[0].Title) 
                    { 
                        Write-Host "     ----------------------------------------------------- " 
                        Write-Host "      Announcements Item[0] to delete:", $MyList.Items[0].Title -foregroundcolor green 
                        Write-Host "       >>>>> List to delete: ", $MyList.Title, " <<<<<" -foregroundcolor green 
                        Write-Host "     ----------------------------------------------------- " 
                        $ListsToDeleteFoSPWeb += $MyList.ID 
                    } 
                    else 
                    { 
                        Write-Host "     ----------------------------------------------------- " 
                        Write-Host "      Announcements Item[0] to check:", $MyList.Items[0].Title -foregroundcolor red 
                        Write-Host "     ----------------------------------------------------- " 
                    } 
                } 
                else 
                { 
                    if($MyList.ItemCount -gt 5000) 
                    { 
                        Write-Host "     ------------------------------------ " 
                        Write-Host "      List Name:", $MyList.Title, "- GUID:", $MyList.ID -foregroundcolor magenta 
                        Write-Host "      >>Items Count:", $MyList.ItemCount -foregroundcolor magenta 
                    } 
                    else 
                    { 
                        #Write-Host "      List Name:", $MyList.Title, "(", $MyList.ItemCount , Items") - GUID:", $MyList.ID  -foregroundcolor green $MyList.BaseTemplate 
                    } 
                } 
            } 
        } 
    }

    
    if(($RemoveBasicEmptyLists) -and ($ListsToDeleteFoSPWeb.count -gt 0)) 
    { 
        foreach($ToDeleteListGUID in $ListsToDeleteFoSPWeb) 
        { 
            Write-Host "      ======================================= " 
            $myweb.Lists.Delete($ToDeleteListGUID) 
            Write-Host "        >>> List GUID deleted:", $ToDeleteListGUID 
            Write-Host "      ======================================= " 
        } 
    } 
    foreach ($subweb in $myweb.GetSubwebsForCurrentUser()) 
    { 
        Check-Lists $subweb.Url 
    } 
    $myweb.Dispose() 
    $mysite.Dispose() 
}

function Check-Large-Empty-Lists([string]$WebAppURL) 
{ 
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") > $null

    $Thesite = new-object Microsoft.SharePoint.SPSite($WebAppURL) 
    $oApp = $Thesite.WebApplication

    foreach ($Sites in $oApp.Sites) 
    { 
        $mySubweb = $Sites.RootWeb 
        Write-Host " -------------------------------------------------------- " 
        #Write-Host "URL", $mySubweb.Url 
        Check-Lists $mySubweb.Url 
        
    } 
    $Thesite.Dispose()

}

cls

Check-Large-Empty-Lists “http://YourSharePointSite2007”

You can see an example of my last execution:

image_thumb_779752BA.png

 

Fabrice Romelard [MVP]

 

Original Message (in french):

2 Replies

can u provide me a powershell script to get all the details of a document by just document url, i have thousands of doc url in csv file and i want a output a csv file that has information like site url, document url, site owner, site owner email id.

Dear Vikrant, 

I think you can develop your owne script using that one as model, but the first starting point is to know exactly what is your configuration.

Because, depending of the SP version (2007, 2010, 2013, 2016, SPO) or model object (SP Model Object, CSOM, ...) you can use, the script will be totally different.

 

You can create directly a new message into that group to ask someone to help you or go directly to the MSDN Forum.

Fabrice