Tuesday, February 27, 2018

SharePoint PowerShell Get InActive Sites Report

I am asked to generate a site report to identify which all site collections/ sites / sub sites are not in use for so long, so that they can be archived.

I choose to write a PowerShell script to visit all the site collections, sites, sub sites and generate a CSV report with below information. Based on the last accessed/modified date, decision making happens whether to archive/delete the site.


Site Title
Site URL
Site Users Count
Site Last Modified
Web Last Modified (item content in a web)
List Title (lists under a web)
List Items Count

Below is the PowerShell script to generate the site report.

$timestamp = get-date -format "yyyyMMdd_hhmmtt"
$filenameStart = "MSHSSiteReport"
$logfile = ("D:\{0}{1}.csv" -f $filenamestart, $timestamp)

$header = "webname,weburl,usercount,sitelastmodified,weblastmodified,archive,listname,itemcount"
$header | out-file -FilePath $logfile

$result = @()

# Get All Web Applications
$WebApps=Get-SPWebApplication
foreach($webApp in $WebApps)
{
    if(!$webApp.Url.StartsWith("https://workspace.mountsinai.org/"))
    {
        continue
    }
    Write-Host $webApp.Url -ForegroundColor Red
    foreach ($SPsite in $webApp.Sites)
    {   
        Write-Host $SPsite.Url -BackgroundColor DarkCyan
       # get the collection of webs
       foreach($SPweb in $SPsite.AllWebs)
        {
            if($SPsite.Url -eq $SPweb.Url)
            { 
                $SizeInKB = $SPsite.Usage.Storage
                $SizeInGB = $SizeInKB/1024/1024 #/1024
                $SizeInGB = [math]::Round($SizeInGB,2)
                Write-Host $SizeInGB MB -ForegroundColor Yellow
            }

            $siteTitle = $SPweb.title
            $siteUrl = $spweb.URL
            $userCount = $SPweb.Users.Count
            $siteLastModified = $SPsite.LastContentModifiedDate
            $webLastModified = $SPweb.LastItemModifiedDate
                                   
            $archive = "False"
            if($SPweb.LastItemModifiedDate -lt ((Get-Date).AddMonths(-12)))
            {
                $archive = "True"  
            }

            write-host $siteTitle ":" $siteUrl ":" $userCount ":" $webLastModified ":" $archive

            $report = New-Object System.Object
         $report | Add-Member -MemberType NoteProperty -Name "Title" -Value $siteTitle
         $report | Add-Member -MemberType NoteProperty -Name "URL" -Value $siteUrl
         $report | Add-Member -MemberType NoteProperty -Name "UserCount" -Value $userCount
         $report | Add-Member -MemberType NoteProperty -Name "SiteLastModified" -Value $siteLastModified
         $report | Add-Member -MemberType NoteProperty -Name "WebLastModified" -Value $webLastModified
         $report  | Add-Member -MemberType NoteProperty -Name "Archive" -Value $archive
         $report | Add-Member -MemberType NoteProperty -Name "List" -Value ""
         $report | Add-Member -MemberType NoteProperty -Name "ItemCount" -Value ""

         $result += $report

      Write-Host ([String]::Format("Procesing web {0}",$SPweb.Url)) -foregroundcolor Yellow
      foreach($l in $SPweb.Lists)
      {
               
           Write-Host ([String]::Format("List, '{0}', has {1} items.",$l.Title, $l.ItemCount)) -foregroundcolor Green
               
                    $report = New-Object System.Object
                 $report | Add-Member -MemberType NoteProperty -Name "Title" -Value ""
                 $report | Add-Member -MemberType NoteProperty -Name "URL" -Value ""
                 $report | Add-Member -MemberType NoteProperty -Name "UserCount" -Value ""
                 $report | Add-Member -MemberType NoteProperty -Name "SiteLastModified" -Value ""
                 $report | Add-Member -MemberType NoteProperty -Name "WebLastModified" -Value ""
                 $report | Add-Member -MemberType NoteProperty -Name "Archive" -Value $archive
                 $report | Add-Member -MemberType NoteProperty -Name "List" -Value $l.Title
                 $report | Add-Member -MemberType NoteProperty -Name "ItemCount" -Value $l.ItemCount

                 $result += $report
              
      } 

        }
    }
  }
 

$result | Export-Csv -NoTypeInformation -Path $logfile


No comments:

Post a Comment