Friday 23 May 2014

PowerShell script to get the List of all Sites, Lists, List Types,Number of items in each list..

param
(

[boolean] $WriteToFile = $true
)

#Get all lists in farm
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Counter variables
$webcount = 0
$listcount = 0
$totalCount = 0
$URL = Read-Host "Enter the website URL"
if($WriteToFile -eq $true)
{
$outputPath = Read-Host "Outputpath (e.g. C:\SiteMap.txt)"
}
if(!$URL)
{
#Grab all webs
$webs = (Get-SPSite -limit all | Get-SPWeb -Limit all -ErrorAction SilentlyContinue)
}
else
{
$webs = Get-SPWeb $URL
}
if($webs.count -ge 1 -OR $webs.count -eq $null)
{
if($WriteToFile -eq $true){Add-Content -Path $outputPath -Value "Total Sites = "$($web.count)}   
foreach($web in $webs)
    {
  
   #Grab all lists in the current web
    $lists = $web.Lists  
    Write-Host "Website"$web.url -ForegroundColor Green
    if($WriteToFile -eq $true){Add-Content -Path $outputPath -Value "Site URL  - "$($web.url)}
        foreach($list in $lists)
        {
            $listcount +=1 
            Write-Host " – "$list.Title         
            if($WriteToFile -eq $true){Add-Content -Path $outputPath -Value "List Name – "$($list.Title)"    List Type - "$($list.BaseType) }
    foreach ($folderItem in $list.Folders)
    {
        
          $totalCount += $folderItem.Folder.ItemCount
    }
    f($WriteToFile -eq $true){Add-Content -Path $outputPath -Value "Total number of Items =  "$totalCount}
        }

    #$webcount +=1
    $web.Dispose()
    }

}
else
{
Write-Host "No webs retrieved, please check your permissions" -ForegroundColor Red -BackgroundColor Black
}

No comments:

Post a Comment