Today I needed a script to give me the last created site collections.
I found a reference here. I modified it a little bit so that it actually works and disposals are being done the right way …
$siteUrl = "<Absolute Url Pointing to a Site Collection in your WebApp>" $date = date $timeInHistory = ($date.AddDays(-5)) # Load Snapins Try { @('Microsoft.SharePoint.PowerShell') | % { $snapinName = $_ if ((Get-PSSnapin | ? {$_.Name -eq $snapinName}) -eq $null) { Write-Host "Adding PSSnapin $snapinName" Add-PSSnapin $snapinName } } } Catch { Write-Host "Error adding Snapin for SharePoint" -foregroundcolor Black -backgroundcolor Red return } Write-Host "" $rootSite = $null $site = $null $web = $null try { $rootSite = New-Object Microsoft.SharePoint.SPSite($siteUrl) $spWebApp = $rootSite.WebApplication foreach($site in $spWebApp.Sites) { foreach($web in $site.AllWebs) { if(($web.created -ne $null) -and ($web.created -gt $timeInHistory)) { [string]$output = $web.url + " " + $web.created.ToShortDateString() write-host $output } $web.Dispose() } $site.Dispose() } $rootSite.Dispose() } finally { if ($web -ne $null) { $web.Dispose() } if ($site -ne $null) { $site.Dispose()} if ($rootSite -ne $null) { $rootSite.Dispose()} }