none
Generate report of inactive sites and owners RRS feed

  • Question

  • Hello -

    Our SP environment has hundreds of sites/subsites, many of which probably haven't been accessed in years.   We are in the process of cleaning up inactive sites by trying to identify which sites haven't been accessed/modified in at least 2 years.   I found the PS query below that will at least give us an idea of sites not updated in that time, as well as who the site owners are so that we can contact them on cleaning up their sites (so that we can prep to move to SP Online).

    With the content db being around 400 GB, I expect this to probably run for a couple of days.   Are there any suggestions on better alternative to create a report on site owners/last accessed date etc?  

    Get-SPSite -Limit All | 
      Get-SPWeb -Limit All | 
      Where { $_.LastItemModifiedDate -lt ((Get-Date).AddMonths(-24)) } |
      foreach {
        # the URL
        ""   #blank line
        $_.url
        "  LastItemModifiedDate: " + $_.LastItemModifiedDate
        "  Web has unique permissions: " + $_.HasUniquePerm
    
        # the Site Collection Administrators
        " Site Collection Administrators:"
        foreach ($user in $_.SiteAdministrators)
        {      
           "  " + $user.DisplayName + " " + $user.UserLogin
        }
    
        # full control users
        " Full Control Users:"
        foreach ($user in $_.Users)
        {
           if ( $_.DoesUserHavePermissions($user,[Microsoft.SharePoint.SPBasePermissions]::FullMask) )      
           { 
             "  " + $user.DisplayName + " " + $user.UserLogin
           }
        }
      }
    Source

    Friday, November 15, 2019 12:57 PM

Answers

  • Hi,

    Try using “$users = $_.Allusers”.

    Here’s an update for your reference.

    Add-PSSnapin Microsoft.SharePoint.PowerShell
    $csv = "C:\Temp\0.csv"
    Write-Output "URL `t Site Admins `t Last Access date" | Out-file $csv
    
    $webs = Get-SPSite "http://sp" | Get-SPWeb -Limit All
    foreach ($web in $webs) {
        
        $users = $web.Allusers
    	write-Host "Processing " $web.url -foregroundcolor cyan
    	$Admins=""
        foreach ($user in $users) {
            if ($web.DoesUserHavePermissions($user,[Microsoft.SharePoint.SPBasePermissions]::FullMask)) {
                write-Output $user.Name
    			$Admins+= $user.Name +";"
            }
        }
    	$Lastaccessed = $web.LastItemModifiedDate
    	Write-Output "$($web.URL) `t $($Admins) `t $($Lastaccessed)" | Out-File $csv -Append
    	$web.Dispose()
    }
    
    #Unload SharePoint snap-in
    Remove-PSSnapin Microsoft.SharePoint.PowerShell
    Write-Host ""
    Write-Host "Script Execution finished" -foregroundcolor green

    Best regards,

    Chelsea Wu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by XRS Thursday, November 21, 2019 1:12 PM
    Thursday, November 21, 2019 8:31 AM

All replies

  • Hi,

    Based on your requirement, I think PowerShell is the best solution to get this report for your farm.

    Here’s another sample script for your reference. It will get you URLs of all site collections in your farm, as well as Site Collection Owner, Admin, Last Access date and Content Modified date.

    #Set file location for saving information. We'll create a tab separated file.
    $FileLocation = "C:\Temp\Report.csv"
    
    #Load SharePoint snap-in
    Add-PSSnapin Microsoft.SharePoint.PowerShell
    
    #Add color
    function Receive-Color{
        process { Write-Host $_ -ForegroundColor Green }
    }
    
    #Fetches webapplications in the farm
    $WebApplications = Get-SPWebApplication -IncludeCentralAdministration
    Write-Output "URL `t Site Collection Owner `t Site Collection Secondary Owner `t Site Collection Admin `t Last Access date `t ContentModified" | Out-file $FileLocation
    foreach($WebApplication in $WebApplications){
        #Fetches site collections list within sharepoint webapplication
        Write-Output ""
        Write-Output "Working on web application $($WebApplication.Url)" | Receive-Color
        $Sites = Get-SPSite -WebApplication $WebApplication -Limit All
        foreach($Site in $Sites){   
            $Admins=""
    		
            #Get all Site Collection Administrators
            foreach ($siteCollAdmin in $Site.RootWeb.SiteAdministrators){
                $Admins+= $siteCollAdmin.LoginName +";"
            }
            foreach($web in $Site.Allwebs){
                $Lastaccessed = $web.LastItemModifiedDate
                $ContentModified = $Site.LastContentModifiedDate
            }
        
            #Fetches information for each  site
            Write-Output "$($Site.Url) `t $($Site.Owner.Name) `t $($Site.SecondaryContact.Name) `t $($Admins) `t $($Lastaccessed) `t $($ContentModified)" | Out-File $FileLocation -Append
            $Site.Dispose()
        }
    }
    
    #Unload SharePoint snap-in
    Remove-PSSnapin Microsoft.SharePoint.PowerShell
    Write-Output ""
    Write-Output "Script Execution finished" | Receive-Color


    Here’s the reference.

    Get last access date/time for all SharePoint site collections with Powershell.

    Please note: Microsoft provides third-party contact information to help you find technical support. This contact information may change without notice. Microsoft does not guarantee the accuracy of this third-party contact information.

    Best regards,

    Chelsea Wu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Monday, November 18, 2019 3:04 AM
  • Thanks Chelsea.  This points me to the right direction.   The query you referenced only brings back info for the web applications, not the sites/subsites.   We have 3 or 4 web applications and I will have to tweak it to query only one web application/site collection.  
    Monday, November 18, 2019 3:30 PM
  • Hi,

    I’ve modified the PowerShell script to run on a specific web application, please see it below.

    $FileLocation = "C:\Temp\Report.csv"
    $webappurl = "http://sp"
    
    Add-PSSnapin Microsoft.SharePoint.PowerShell
    
    #Add color
    function Receive-Color{
        process { Write-Host $_ -ForegroundColor Green }
    }
    
    #Fetches webapplications in the farm
    $WebApplication = Get-SPWebApplication $webappurl
    Write-Output "URL `t Site Collection Owner `t Site Collection Secondary Owner `t Site Collection Admin `t Last Access date `t ContentModified" | Out-file $FileLocation
    Write-Output ""
    Write-Output "Working on web application $($WebApplication.Url)" | Receive-Color
    $Sites = Get-SPSite -WebApplication $WebApplication -Limit All
    foreach($Site in $Sites){   
        $Admins=""
        foreach ($siteCollAdmin in $Site.RootWeb.SiteAdministrators){
            $Admins+= $siteCollAdmin.LoginName +";"
        }
        foreach($web in $Site.Allwebs){
            $Lastaccessed = $web.LastItemModifiedDate
            $ContentModified = $Site.LastContentModifiedDate
        }
        
        Write-Output "$($Site.Url) `t $($Site.Owner.Name) `t $($Site.SecondaryContact.Name) `t $($Admins) `t $($Lastaccessed) `t $($ContentModified)" | Out-File $FileLocation -Append
        $Site.Dispose()
    }
    
    #Unload SharePoint snap-in
    Remove-PSSnapin Microsoft.SharePoint.PowerShell
    Write-Output ""
    Write-Output "Script Execution finished" | Receive-Color

    Best regards,

    Chelsea Wu

    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Tuesday, November 19, 2019 10:39 AM
  • Thanks. The csv output only contains five or six lines with the info for the site collections, but not the actual sites within the site collections.  Example below.

    I am only interested in the first site collection and need to get ever site/subsite underneath that (e.g., http://sharepoint/finance, http://sharepoint/legal, etc).  

    http://sharepoint/

    http://sharepoint/sites/

    http://sharepoint/sites/Search

    http://sharepoint/sites/BI

    http://sharepoint/sites/test

    Tuesday, November 19, 2019 2:22 PM
  • OK I can get it almost work using script below. Results come in like this.  It only lists Full Control Users if they are explicitly listed in _layouts/15/user.aspx.   If they are part of an SP group that has Full Control users, those do not show up.  

    http://sharepoint/legal/test
      LastItemModifiedDate: 07/29/2017 04:00:24
      Web has unique permissions: True
     Site Collection Administrators:
      Doe, John i:0#.w|DOM1\E145987
      spadmin i:0#.w|DOM2\spadmin
      Doe, Jane i:0#.w|DOM1\E213450
     Full Control Users:

    Get-SPSite -Identity "http://sharepoint" | 
      Get-SPWeb -Limit All | 
      Where { $_.LastItemModifiedDate -lt ((Get-Date).AddMonths(-24)) } |
      foreach {
        # the URL
        ""   #blank line
        $_.url
        "  LastItemModifiedDate: " + $_.LastItemModifiedDate
        "  Web has unique permissions: " + $_.HasUniquePerm
    
        # the Site Collection Administrators
        " Site Collection Administrators:"
        foreach ($user in $_.SiteAdministrators)
        {      
           "  " + $user.DisplayName + " " + $user.UserLogin
        }
    
        # full control users
        " Full Control Users:"
        foreach ($user in $_.Users)
        {
           if ( $_.DoesUserHavePermissions($user,[Microsoft.SharePoint.SPBasePermissions]::FullMask) )      
           { 
             "  " + $user.DisplayName + " " + $user.UserLogin
           }
        }
      }

    Wednesday, November 20, 2019 2:17 PM
  • Hi,

    Try using “$users = $_.Allusers”.

    Here’s an update for your reference.

    Add-PSSnapin Microsoft.SharePoint.PowerShell
    $csv = "C:\Temp\0.csv"
    Write-Output "URL `t Site Admins `t Last Access date" | Out-file $csv
    
    $webs = Get-SPSite "http://sp" | Get-SPWeb -Limit All
    foreach ($web in $webs) {
        
        $users = $web.Allusers
    	write-Host "Processing " $web.url -foregroundcolor cyan
    	$Admins=""
        foreach ($user in $users) {
            if ($web.DoesUserHavePermissions($user,[Microsoft.SharePoint.SPBasePermissions]::FullMask)) {
                write-Output $user.Name
    			$Admins+= $user.Name +";"
            }
        }
    	$Lastaccessed = $web.LastItemModifiedDate
    	Write-Output "$($web.URL) `t $($Admins) `t $($Lastaccessed)" | Out-File $csv -Append
    	$web.Dispose()
    }
    
    #Unload SharePoint snap-in
    Remove-PSSnapin Microsoft.SharePoint.PowerShell
    Write-Host ""
    Write-Host "Script Execution finished" -foregroundcolor green

    Best regards,

    Chelsea Wu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by XRS Thursday, November 21, 2019 1:12 PM
    Thursday, November 21, 2019 8:31 AM
  • Thanks!!  This seems to work out just fine.
    Thursday, November 21, 2019 1:13 PM
  • Hi, 

    Thanks for marking my reply as the answer to this thread. I’m glad to know the information is helpful to you.

    Here I will provide a summary of this post for your information.

    [Generate report of inactive sites and owners - Summary]

    Expectation
    ====================
    Generate a report on inactive sites and site owners with PowerShell

    Environment
    ====================
    SharePoint 2013

    Solution
    ====================
    Here’s a sample script to generate a csv report on subsites under a specific site.

    Add-PSSnapin Microsoft.SharePoint.PowerShell
    $csv = "C:\Temp\0.csv"
    Write-Output "URL `t Site Admins `t Last Access date" | Out-file $csv
    
    $webs = Get-SPSite "http://sp" | Get-SPWeb -Limit All
    foreach ($web in $webs) {
        $users = $web.Allusers
    	write-Host "Processing " $web.url -foregroundcolor cyan
    	$Admins=""
        foreach ($user in $users) {
            if ($web.DoesUserHavePermissions($user,[Microsoft.SharePoint.SPBasePermissions]::FullMask)) {
            write-Output $user.Name
    	$Admins+= $user.Name +";"
            }
        }
    	$Lastaccessed = $web.LastItemModifiedDate
    	Write-Output "$($web.URL) `t $($Admins) `t $($Lastaccessed)" | Out-File $csv -Append
    	$web.Dispose()
    }
    
    #Unload SharePoint snap-in
    Remove-PSSnapin Microsoft.SharePoint.PowerShell
    Write-Host ""
    Write-Host "Script Execution finished" -foregroundcolor green

    Best regards,
    Chelsea Wu


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Friday, November 22, 2019 1:44 AM