none
Powershell to get document modified date RRS feed

  • Question

  • Hello. 

    Is there a Powershell script that I can use that would list all documents and its modified date from a document library?  I'd preferably like to run this on a site that has several subsites and all shared document libraries would need to retrieve this information. 

    I was thinking something like script below, but that gives me an error: 

    Exception calling "GetItemById" with "1" argument(s): "Item does not exist. It
    may have been deleted by another user."

    Add-PSSnapin Microsoft.SharePoint.Powershell $web = Get-SPWeb "http://sharepoint/test" $list = $web.Lists["Documents"] $item = $list.GetItemById(1) $lastWriteTime = $item.TimeLastModified function Get-DocInventory([string]$siteUrl) { $web = Get-SPWeb "http://sharepoint/test" foreach ($list in $web.Lists) { foreach ($item in $list.Items) { foreach($version in $item.Versions){ $data = @{

    "Site URL" = $web.URL "Version" = $version.VersionLabel "List Name" = $list.Title "Created By" = $item["Author"] "Created Date" = ($item["Created"] -as [datetime]).DateTime "Modified By" = $version["Editor"] "Modified Date" = ($version["Modified"] -as [datetime]).DateTime "Item Name" = $item.Name } New-Object PSObject -Property $data | Select "Site URL", "List Name", "Item Name", "Version", "Created By", "Created Date", "Modified By", "Modified Date" } } $web.Dispose(); } }





    • Edited by XRS Thursday, May 14, 2020 5:26 PM
    Thursday, May 14, 2020 3:05 PM

Answers

  • Hello,

    I got you. Your site collection is http://sharepoint, and http://sharepoint/test is one of the sites under the site collection. What you want is information in this site http://sharepoint/test and all subsites under this site. Am I right?

    If so, just add the filter in the script:

    if($web.url -like "http://sharepoint/test*")

    The whole script:

    function Get-DocInventory([string]$siteUrl) {
      $site = New-Object Microsoft.SharePoint.SPSite $siteUrl
      foreach($web in $site.allwebs){
      if($web.URL -like "http://sharepoint/test*"){
      foreach ($list in $web.Lists) {  
        foreach ($item in $list.Items) {
          foreach($version in $item.Versions){
            $data = @{
              "Site URL" = $web.URL
              "Version" = $version.VersionLabel
              "List Name" = $list.Title
              "Created By" = $item["Author"]
              "Created Date" = ($item["Created"] -as [datetime]).DateTime
              "Modified By" = $item["Editor"]
              "Modified Date" = ($item["Modified"] -as [datetime]).DateTime
              "Item Name" = $item.Name
            }
            New-Object PSObject -Property $data | Select "Site URL", "List Name", "Item Name", "Version", "Created By", "Created Date", "Modified By", "Modified Date"
          }
        }
        $web.Dispose();
      }
      }
      }
      $site.Dispose()
    }
    
    Get-DocInventory("http://sharepoint") | Export-Csv -NoTypeInformation -Path E:\temp\GenerateReport.csv

    Best Regards,

    Michael Han


    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 Monday, May 18, 2020 11:58 AM
    Monday, May 18, 2020 3:09 AM
    Moderator

All replies

  • This script works, but it only allows me to do one subsite at a time.   I'd like to cycle through each subsite if possible (e.g., http://sharepoint/test/1, http://sharepoint/test/2, etc) and output the results in one file. thanks.

    function Get-DocInventory([string]$siteUrl) { #$site = New-Object Microsoft.SharePoint.SPSite $siteUrl $web = Get-SPWeb "http://sharepoint/test" foreach ($list in $web.Lists) { foreach ($item in $list.Items) { foreach($version in $item.Versions){ $data = @{

    "Site URL" = $web.URL "Version" = $version.VersionLabel "List Name" = $list.Title "Created By" = $item["Author"] "Created Date" = ($item["Created"] -as [datetime]).DateTime "Modified By" = $item["Editor"] "Modified Date" = ($item["Modified"] -as [datetime]).DateTime "Item Name" = $item.Name } New-Object PSObject -Property $data | Select "Site URL, "List Name", "Item Name", "Version", "Created By", "Created Date", "Modified By", "Modified Date" } } $web.Dispose(); } #$site.Dispose() } Get-DocInventory | Export-Csv -NoTypeInformation -Path E:\temp\GenerateReport.csv





    • Edited by XRS Thursday, May 14, 2020 5:27 PM
    Thursday, May 14, 2020 3:29 PM
  • Try with the below sample script.

    #Load SharePoint Assemblies
    Add-PSSnapin Microsoft.SharePoint.PowerShell
    
    $siteUrl=”http://portal.contoso.com/sites/test;
    $site=new-object Microsoft.SharePoint.SPSite($siteURL)
    
    #Enter the date you want to compare, leave the date blank if you want the current date.
    $date = Get-Date 8/22/2012
    
    foreach($web in $site.AllWebs)
    {
        “Site ” + $web.Url >> c:\temp\LastModified.txt
        
        foreach($list in $web.Lists)
        {
            if($list.LastItemModifiedDate -gt $date) #if list item modified date is greater than $date write to text file
            {
                “—-List” + $list.Title >> c:\temp\LastModified.txt
                “——–Last Modified” + $list.LastItemModifiedDate >> c:\temp\LastModified.txt
            }
        }
    web.Dispose()
    }
    site.Dispose()

    Below article for your reference:

    https://jshidell.wordpress.com/2012/08/22/getting-the-last-modified-date-for-all-listdocument-libraries-in-a-sharepoint-2007-site-collection-using-powershell/

    Thanks & Regards,


    sharath aluri

    Thursday, May 14, 2020 5:50 PM
  • Hello,

    Make a little change on your script, this should work for you:

    function Get-DocInventory([string]$siteUrl) {
      $site = New-Object Microsoft.SharePoint.SPSite $siteUrl
      foreach($web in $site.allwebs){
      foreach ($list in $web.Lists) {
        foreach ($item in $list.Items) {
          foreach($version in $item.Versions){
            $data = @{
              "Site URL" = $web.URL
              "Version" = $version.VersionLabel
              "List Name" = $list.Title
              "Created By" = $item["Author"]
              "Created Date" = ($item["Created"] -as [datetime]).DateTime
              "Modified By" = $item["Editor"]
              "Modified Date" = ($item["Modified"] -as [datetime]).DateTime
              "Item Name" = $item.Name
            }
            New-Object PSObject -Property $data | Select "Site URL", "List Name", "Item Name", "Version", "Created By", "Created Date", "Modified By", "Modified Date"
          }
        }
        $web.Dispose();
      }
      }
      $site.Dispose()
    }
    
    Get-DocInventory("http://sharepoint") | Export-Csv -NoTypeInformation -Path E:\temp\GenerateReport.csv

    Best Regards,

    Michael Han


    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, May 15, 2020 2:18 AM
    Moderator
  • Thanks Michael.  Would this run against all sites in the site collection?  

    I would need it to run against only one site and it's subsites, below, which I see what removed from the code.  

    $web = Get-SPWeb "http://sharepoint/test"

    Friday, May 15, 2020 12:55 PM
  • Yes above Michael code will run for all sites in the Site Collection. yes he remove the subsite and added Site Collection URL to it and looped all the subsites in it.

    Thanks & Regards,


    sharath aluri

    Friday, May 15, 2020 1:10 PM
  • Yeah that won't work.   We have one site collection and its huge.  I need to work against individual sites and their subsites.  Thanks.

    By the way, I tried the script you listed earlierand it throws this error. 

    At E:\temp\mod.ps1:9 char:6
    +     "Site " + $web.Url >> E:\temp\LastModified.txt
    +      ~~~~
    Unexpected token 'Site' in expression or statement.
    At E:\temp\mod.ps1:16 char:30
    +             "---Last Modified" + $list.LastItemModifiedDate >>
    c:\temp\LastModif ...
    +
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    The string is missing the terminator: ".
        + CategoryInfo          : ParserError: (:) [], ParseException
        + FullyQualifiedErrorId : UnexpectedToken


    • Edited by XRS Friday, May 15, 2020 1:58 PM
    Friday, May 15, 2020 1:58 PM
  • Michael already modified your script which suits exactly for your scenario and just tested and it works fine you can use his modified script. seems some quotes are missing from the above provided script you can ignore mine will update later.

    Thanks & Regards,


    sharath aluri

    Friday, May 15, 2020 2:33 PM
  • I can't run it against the entire site collection.   We have one site collection with hundreds of sites.   It's not a good setup I know.   But we're working on migrating to SPO.   I need to limit results to a specific site and its subsites only.
    Friday, May 15, 2020 3:25 PM
  • But you asked for a Site Collection only right ? What exactly you need please let us know clearly, you need for subsite or site collection ?

    Thanks & Regards,


    sharath aluri

    Friday, May 15, 2020 5:17 PM
  • I mentioned in the original post that I'd like to run this against a site and its subsites.   I don't want to run it against the entire site collection that contains 800+ sites.  That CSV result file would be ridiculous.  I just need to run it against one site that has multiple subsites.
    Friday, May 15, 2020 5:36 PM
  • Hello,

    I got you. Your site collection is http://sharepoint, and http://sharepoint/test is one of the sites under the site collection. What you want is information in this site http://sharepoint/test and all subsites under this site. Am I right?

    If so, just add the filter in the script:

    if($web.url -like "http://sharepoint/test*")

    The whole script:

    function Get-DocInventory([string]$siteUrl) {
      $site = New-Object Microsoft.SharePoint.SPSite $siteUrl
      foreach($web in $site.allwebs){
      if($web.URL -like "http://sharepoint/test*"){
      foreach ($list in $web.Lists) {  
        foreach ($item in $list.Items) {
          foreach($version in $item.Versions){
            $data = @{
              "Site URL" = $web.URL
              "Version" = $version.VersionLabel
              "List Name" = $list.Title
              "Created By" = $item["Author"]
              "Created Date" = ($item["Created"] -as [datetime]).DateTime
              "Modified By" = $item["Editor"]
              "Modified Date" = ($item["Modified"] -as [datetime]).DateTime
              "Item Name" = $item.Name
            }
            New-Object PSObject -Property $data | Select "Site URL", "List Name", "Item Name", "Version", "Created By", "Created Date", "Modified By", "Modified Date"
          }
        }
        $web.Dispose();
      }
      }
      }
      $site.Dispose()
    }
    
    Get-DocInventory("http://sharepoint") | Export-Csv -NoTypeInformation -Path E:\temp\GenerateReport.csv

    Best Regards,

    Michael Han


    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 Monday, May 18, 2020 11:58 AM
    Monday, May 18, 2020 3:09 AM
    Moderator
  • Michael Han, the man with the master plan!    Thank you very much for this update!!!  Works perfectly.

    :thumbsup:

    Monday, May 18, 2020 11:59 AM