Saturday, 4 July 2015

Powershell Get SharePoint Site Collections Size

Please run the below mentioned script but before that create input file of the environment:


File Name:
QAAllSites.csv


Content:
Site   URL
ABC http:\\Webapp1\sites\SC1
DEF  http:\\WebApp1\sites\SC2


Script:




#First steps to pass parameter values


param(
[Parameter(Mandatory = $true, valueFromPipelineByPropertyName = $true )][system.string] $env,
[Parameter(Mandatory = $true, valueFromPipelineByPropertyName = $true)][system.string] $outputFilePath
)


#Check SharePoint snapin is added or not?


$snapin = Get-PSSnapin | Where-Object {$_.Name = ""Microsoft.SharePoint.PowerShell"}
if($snapin -eq $null)
{
     Write-Host "Adding 'Microsoft.SharePoint.PowerShell' snapin..."
      Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}


#create variable to get current date time


$startDate = Get-Date
Write-Host "Script start running.." $startDate -ForegroundColor Yellow




#Create table for binding result


$table = New-Object System.Data.DataTable ["Site Collection Size Table"]
$Title = New-Object System.Data.DataColumn Title,[(system.string)]
$URL = New-Object System.Data.DataColumn URL,[(system.string)]
$Size_In_MB = New-Object System.Data.DataColumn Size_In_MB,[(system.string)]


#Check the environment for which script is going to run(eg: QA, Pre-QA)


if($env -eq "QA")
{
    $FileName = "QAAllSites.csv"
}
elseif($env -eq "Pre-QA")
{
   $FileName = "Pre-QAAllSites.csv"
}
else
{
    Write-Host "Choose the correct environment name!"
    return
}


$getFileContent = Import-csv -path $FileName
$getSites  = $getFileContent.URL


Foreach($getSite in $getSites)
{
           Write-Host "Fetching size details of site..." $getSite -Foreground Green
  try{
           $Site = Get-SPSite -Identity $getSite
            $row = $table.NewRow()
            $row.Title = $Site.RootWeb.Title
            $row.URL = $Site.URL
            $row.Size_In_MB = $Site.usage.storage/1MB
            $table.Rows.Add($row)
            $table.AcceptChanges()
            $Site.Dispose()
}
Catch{
             $_.Exception.Message -ForegroundColor Red
}
}


#Export result


$table | Export-csv -path $outputPath -NoTypeInformation


#Get the end date & time of the script


$endDate = Get-Date
Write-Host "Script Completed successfully, Please verify the output file"






##To run the above script pass the command:


.\GetSiteCollectionSizes -env "QA" -outputPath "QAReport.CSV"








###Incase of query, please write to me on Arpit_Mohan@outlook.com

No comments:

Post a Comment