SharePoint Online: Get Site Collection Created Date using PowerShell

Requirement: Get Site Collection creation date in SharePoint Online.

SharePoint Online: PowerShell to Get Site Creation Date
How to check SharePoint online site creation date? Well, there is no UI to get site creation date in SharePoint Online or in On-Premises. However, PowerShell can help to find SharePoint online site collection created date.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Set parameter values
$SiteURL="https://crescent.sharepoint.com/sites/Ops"

Try { 
        #Get Credentials to connect
        $Cred= Get-Credential
        $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
        #Setup the context
        $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
        $Ctx.Credentials = $Credentials
        
        #Get the Web
        $Web = $Ctx.Web
        $Ctx.Load($web)
        $Ctx.ExecuteQuery()

        #sharepoint online powershell get site creation date
        Write-host -f Green "Site Collection Created Date:"$Web.created.toShortDateString()
     }
    Catch {
        write-host -f Red "Error Getting Site Collection Creation Date!" $_.Exception.Message
   }
This PowerShell gets the site collection created date!

Get Created Date of All SharePoint Online Site Collections in Office 365 Tenant:
Let's get created date of all site collections in the tenant.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll"

#Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$ReportOutput="C:\Temp\SiteCreatedDate.csv"
 
Try {
    #Get Credentials to connect
    $Cred = Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
 
    #Connect to SharePoint Online Admin Center
    Connect-SPOService -Url $AdminSiteURL –Credential $Cred
 
    #Get All site collections
    $SiteCollections = Get-SPOSite -Limit All
    Write-Host "Total Number of Site collections Found:"$SiteCollections.count -f Yellow
 
    #Array to store Result
    $ResultSet = @()
 
    #Loop through each site collection and retrieve details
    Foreach ($Site in $SiteCollections)
    {
        Write-Host "Processing Site Collection :"$Site.URL -f Yellow
 
        Try {
            #Get the Root web of the Site collection
            $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Site.URL)
            $Ctx.Credentials = $Credentials  
            $RootWeb=$Ctx.Web
            $Ctx.Load($RootWeb)
            $Ctx.ExecuteQuery()
            $CreatedDate = $RootWeb.created.toShortDateString()
        }
        Catch {    
            write-host -f Red "`tError Getting Root Web:" $_.Exception.Message
        }

        #Get site collection details    
        $Result = new-object PSObject
        $Result | add-member -membertype NoteProperty -name "Title" -Value $Site.Title
        $Result | add-member -membertype NoteProperty -name "Url" -Value $Site.Url
        $Result | add-member -membertype NoteProperty -name "CreatedDate" -Value ($CreatedDate)
        $ResultSet += $Result
    }  
    #Export Result to csv file
    $ResultSet |  Export-Csv $ReportOutput -notypeinformation  
    Write-Host "Site Creation Date Report Generated Successfully!" -f Green
}
Catch {    
    write-host -f Red "Error:" $_.Exception.Message
}

PnP PowerShell to Get Site Collection Created Date in SharePoint Online:
#Config Variable
$SiteURL = "https://crescent.sharepoint.com/Sites/Marketing"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#Get the Root Web with "created" property
$Web = Get-PnPWeb -Includes Created

#Get Site Collection Created Date
Write-host "Site Collection Created on:"$Web.Created 

Let's get all Site collections sort by created date
#Parameters
$TenantURL =  "https://crescent.sharepoint.com"
 
#Get Credentials to connect
$Credential = Get-Credential
 
#Frame Tenant Admin URL from Tenant URL
$TenantAdminURL = $TenantURL.Insert($TenantURL.IndexOf("."),"-admin")

#Connect to Admin Center
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Credential
 
#Get All Site collections - Filter BOT and MySite Host
$Sites = Get-PnPTenantSite -Filter "Url -like '$TenantURL'"

#Iterate through all sites
$SiteInventory = @()
$Sites | ForEach-Object {
    #Connect to each site collection
    $SiteConn = Connect-PnPOnline -Url $_.URL -Credentials $Credential -ReturnConnection

    #Get the Root Web with "created" property
    $Web = Get-PnPWeb -Includes Created

    #Collect Data
    $SiteInventory += New-Object PSObject -Property  ([Ordered]@{
        "Site Name"  = $Web.Title
        "URL "= $Web.URL
        "Created Date" = $Web.Created
    })

    Disconnect-PnPOnline -Connection $SiteConn
}
#Sort Site Collection - Sort by Creation Date
$SiteInventory | Sort-Object 'Created Date' -Descending

4 comments:

  1. How can you capture this for all site collections?

    ReplyDelete
  2. Hi, is there a way to capture the created date of a subsite too?

    ReplyDelete
    Replies
    1. Yes, the script actually gets the created date of the "Root Web", So just enter the $SiteURL variable value to a Subsite URL!

      Delete
  3. Hello! I see that some of your scripts "Filter BOT and MySite Host", which leads me to believe that you may know more about the purpose of the BOT sites. For example, when I run the command Get-SPOSite I have a site that looks like this example: "http://bot19###-002/sites/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

    I've had a ticket open with Microsoft for a couple of weeks and I'm not getting any answers! Thanks for any assistance.

    ReplyDelete

Please Login and comment to get your questions answered!

Powered by Blogger.