Pages

Search This Blog

Wednesday, April 1, 2015

Remove all items from a List in SharePoint through PowerShell


We can use below script to clean data from multiple lists in a SharePoint site. This could be very helpful while developing solutions in SharePoint as we may need to clean the data many times during development. This script requires path to a text file which contains the list names ( each name should be in a new line ).

Copy paste below script in a text file and rename the file extension to - *.ps1 

Write-Host
Write-Host

write-host "Enter the SharePoint Site URL : "
[string]$URL = Read-Host
[string]$WebURL =  $URL.Trim()


$objSite = Get-SPWeb $WebURL -ErrorAction SilentlyContinue


if($objSite -eq $null)
{
               Write-Host
               Write-Host "Site URL not correct or Site does not extsts. Re-run the script with correct URL." -foregroundcolor Red
               Read-Host
}
else
{
        Write-Host
        Write-Host
           write-host "Enter the Text File Path that contains Name of Lists to be cleared : "
        [string]$FilePathTmp = Read-Host
        [string]$FilePath =  $FilePathTmp.Trim().Replace('"','')
        $ListNames = Get-Content -Path $FilePath
        if($ListNames -eq $null)
        {
            Write-Host
               Write-Host "Sorry! Unable to get contents from file." -foregroundcolor Red
               Read-Host
        }
        else
        {
            Write-Host
            write-host  "["  $ListNames.count  "] List Name(s) found in file. " -foregroundcolor Green
            Write-Host
            if( $ListNames.count -gt 0)
            {
                [bool] $found=$false
                foreach ($ListName in $ListNames)
                {
                    if($ListName -ne $null)
                    {
                        $list = $objSite.lists | where {$_.title -eq $ListName}
                        if($list -ne $null)
                        {
                            $found=$true
                            Write-Host $ListName " Found ...." -foregroundcolor Green
                            Write-host "---- List $($list.title) has $($list.items.count) entries"
                            $items = $list.items
                            foreach ($item in $items)
                            {
                                $list.getitembyid($Item.id).Delete()
                            }
                            Write-Host "---- List " $ListName " cleared successfully...." -foregroundcolor Green
                        }
                        else
                        {
                            Write-Host
                            Write-Host $ListName " NOT FOUND in current site...." -foregroundcolor Red
                        }
                    }
                }
                if($found -eq $true)
                {
                    Write-Host
                    Write-Host "Lists Cleared Sucessfully.... Press Enter to exit." -foregroundcolor Yellow
                       Read-Host
                }
            }
            else
            {
                Write-Host
                Write-Host "No List Name found in file.... Press Enter to exit." -foregroundcolor Red
                   Read-Host
            }
           
        }
        $objSite.Dispose()
}



No comments:

Post a Comment