Thursday, December 8, 2016

Get site collection usage report using PowerShell

Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue

$sizeLog = "C:\SiteUsage.csv"
$webApps = Get-SPWebApplication
$arr = @()
foreach($webApp in $webApps)
{
    $sites = Get-SPSite -WebApplication $webApp -Limit All
    foreach($site in $sites)
    {
        $sizeInKB = $site.Usage.Storage
        $sizeInMB = $sizeInKB/1024/1024
        $sizeInMB = [math]::Round($sizeInMB,2)

        $obj = New-Object System.Object
        $obj | Add-Member -type NoteProperty -name "Title" -value $site.RootWeb.Title
        $obj | Add-Member -type NoteProperty -name "Site URL" -value $site.URL      
        $obj | Add-Member -type NoteProperty -name "Database" -Value $site.ContentDatabase.Name
        $obj | Add-Member -type NoteProperty -name "Size in MB" -Value $sizeInMB
        $obj | Add-Member -type NoteProperty -name "Last Access Date" -Value $site.LastContentModifiedDate
     
        $arr += $obj
    }
}
$arr | Export-Csv $sizeLog -Delimiter "," -NoTypeInformation
Write-Host "Exported to" $sizeLog
$site.Dispose()