Get a List SharePoint Sites Recently Modified using PowerShell

On many occasions, my team has been asked to produce a list of all SharePoint sites we had in our Web Application​, as well as the date there was any action on the site (list item or documents CRUD operations). The idea behind the following script is that a user would input a date, and that the PowerShell script will list all workspaces that have had some action after that date. Now, I understand that I most cases, you may wish to get the opposite information (sites that haven’t been touched since a specific date). This information can be obtained by making minor modifications to the script.

Let’s begin! The property that is key for achieving what we are trying to accomplish, is the .ItemLastModifiedDate property of the SPWeb object. This property returns a Date object that indicates the date at which any list item or document was last modified within the site. With that information in hand, all we have to do from there is to compare it against the date the user input in the script and make a decision whether the last modified date is newer or not. One big caveat here is that very often, Cummulative Updates or Service Packs will automatically set the last modified dates of all libraries and lists across all sites to the date they were installed. Please take this into consideration when doing your tests.

$dateInput = Read-Host “Please enter a Date”)

$date = (Get-Date $dateInput)

$url = Read-Host “Please enter the URL of your Web Application”

$webApp = Get-SPWebApplication $url

foreach($site in $webApp.Sites)

{

foreach($web in $site.AllWebs)

{

​if($web.ItemLastModifiedDate -lt $date)

{

​Write-Host “Site Found:” $web.Url

}

$web.Dispose()

}

$site.Dispose()

}

Microsoft Premier Field Engineer - SharePoint

Leave a Comment

Your email address will not be published. Required fields are marked *

*
*