Wednesday, September 19, 2012

Find All Documents Created or Modified by a Particular User in Specific Date Time

Requirement is to find all documents which are uploaded to the SharePoint environment during the past one Month. PowerShell can do the reporting well. Lets see the code:

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

# Set the date Filter
$dateFilter = (Get-Date).AddMonths(-1) #Past Month
"File, Created Time, File Size" | out-file NewDocuments.csv

# Get all Webs
$webs = Get-SPWebApplication "http://sharepoint.crescent.com" | Get-SPSite -Limit All | Get-SPWeb -Limit All

#Iterate through webs
ForEach ($web in $webs) 
{
   #Iterate through All Lists
    ForEach ($list in $web.Lists) 
 {
  #Check for Document Libraries  
        If ($list.BaseType -eq "DocumentLibrary") 
  {
     #Iterate through All documents
                   ForEach ($item in $list.Items)
   {
                  If ($item.URL.StartsWith("_")) {Break} #Skip _catalogs, etc
                  If ($item.URL.EndsWith(".aspx")) {Break} #Skip Form pages
                    If ($item.File.TimeCreated -ge $dateFilter) 
    {
                      $result = """$($web.URL)/$($item.URL)"", $($item.File.TimeCreated), $( [Math]::Round($item.File.Length/1024,2))"
                      $result | Out-File NewDocuments.csv -Append
                 }
                }
        }
    }
}


Same way, we can find all documents uploaded to SharePoint sites based on Date criteria by just checking $item.File.TimeLastModified property. Say For instance,
  • You may be interested to see all old documents created an year back 
  • All documents which are not changed during the past one year.

Find All Documents Created or Modified by a Particular User:
In an another situation, the request is to find all documents either created by  modified by a particular user in SharePoint 2007.
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Region MOSS2007-CmdLets
Function global:Get-SPWebApplication($WebAppURL)
{ 
 if($WebAppURL -eq $null)  #Get All Web Applications
    {
  #Sharepoint 2007 powershell spfarm
  $Farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
  $websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}
  $WebApps = @()
  foreach ($websvc in $websvcs) {
      foreach ($WebApp in $websvc.WebApplications) {
          $WebApps += $WebApp
      }
  }
  return $WebApps
 }
  else #Get Web Application for given URL
 {
    return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)
  }
 }
 
Function global:Get-SPSite()
{
  Param( [Parameter(Mandatory=$true)] [string]$SiteCollURL )
 
   if($SiteCollURL -ne '')
    {
    return new-Object Microsoft.SharePoint.SPSite($SiteCollURL)
   }
}

Function global:Get-SPWeb()
{
 Param( [Parameter(Mandatory=$true)] [string]$SiteURL )
  $site = Get-SPSite($SiteURL)
        if($site -ne $null)
            {
               $web=$site.OpenWeb();
            }
    return $web
}
#EndRegion


# Set the date Filter
"File Name `t Created By `t Modified By `t Created `t Last Modified `t File Size `t URL" | out-file AllDocsByUser.csv
 
# Get the Web Application 
$WebApp = Get-SPWebApplication "http://sharepoint.company.com"

#Iterate through site collections
ForEach ($site in $WebApp.Sites)
    {
     #Iterate through webs
     ForEach ($web in $site.AllWebs)
        {
            #Iterate through All Lists
            ForEach ($list in $web.Lists)
            {
            #Check for Document Libraries 
            If ($list.BaseType -eq "DocumentLibrary")
               {
                #Iterate through All documents
                ForEach ($item in $list.Items)
                    {
                        If ($item.URL.StartsWith("_")) {Break} #Skip _catalogs, etc
                        If ($item.URL.EndsWith(".aspx")) {Break} #Skip Form pages
                      
                        If ( ($item.File.Author -like "*domain\user*") -or ($item.File.ModifiedBy -like "*domain\user*"))
                           {
                             $result = "$($item.File.Name) `t $($item.File.Author) `t $($item.File.ModifiedBy) `t $($item.File.TimeCreated) `t $($item.File.TimeLastModified) `t  $( [Math]::Round($item.File.Length/1024,2)) `t $($web.URL)/$($item.URL)"
                              $result | Out-File AllDocsByUser.csv -Append
                           }
                     }
                }
           }
      }
  }



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...