none
SharePoint Online Change Modified Date to Match file's Last Saved Date RRS feed

  • Question

  • Hello,

    I am trying to use PowerShell to overwrite my file's modified dates. By accident they were all reset when a few folders were renamed and we primarily use "Open in Explorer" to navigate and open files. Here is the script I have cobbled together. The two problems I am still trying to solve are how to iterate through every file in the document library, and how to edit the modified date. Right now it only runs through the root folder.

    $loadInfo1 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
    $loadInfo2 = [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
    $webUrl = "SITE"
    $username = "USER"
    $password = "PASS" | ConvertTo-SecureString -AsPlainText -Force
    $sourceListName = "Documents"
    $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($webUrl) 
    $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)
    
    $web = $ctx.Web
    $list = $web.Lists.GetByTitle('Documents') 
    
    $query = New-Object Microsoft.SharePoint.Client.CamlQuery
                  
    $items = $list.GetItems($query)
    
    $ctx.Load($list)
    $ctx.Load($items);
    $ctx.ExecuteQuery()
    
    for($j=0; $j -lt $items.Count; $j++)
        {
        	
        Write-host "$j["Created"], $itemki["Modified"] "
            $itemki["Modified"]=$itemki["Last Saved Date"]
            $itemki[$j].UpdateOverwriteVersion()
        }

    Thank you in advance for any help!



    Friday, February 12, 2016 12:40 AM

Answers

  • Alright, through Google I found the solution to this problem:

    - Map the SharePoint Document Library as a network drive (Webdav)

    - Here is the script for Word Docs:

    $includeExtensions = @(".docx", ".doc") 
    $path = "T:\*"
    $docs = Get-ChildItem -Path $path -Recurse | ?{$includeExtensions -contains $_.Extension}
    
    foreach($doc in $docs) {
        $application = New-Object -ComObject word.application
        $application.Visible = $false
        try {
            $document = $application.documents.open($doc.FullName, $null, $null, $null, "")
            
        $binding = "System.Reflection.BindingFlags" -as [type]
        $properties = $document.BuiltInDocumentProperties
    
        $lastsavetime = $null
        $creationdate = $null
    
        foreach($property in $properties)
        {
         $pn = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null)
          trap [system.exception]
           {
            continue
           }
           if($pn -eq "Last save time") {
                $lastsavetime = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
           } elseif ($pn -eq "Creation date") {
                $creationdate = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
           }                
        }
    
        $document.Close($false)
        $application.quit()
    
        "Setting " + $doc.FullName
        Set-ItemProperty $doc.FullName -Name "Creationtime" -Value $creationdate 
        Set-ItemProperty $doc.FullName -Name "LastWriteTime" -Value $lastsavetime 
    
        } catch { Write-Host "$document is password protected!" 
                       $document.Close($false) }
    }

    Here is the script for PPTs:

    $includeExtensions = @(".ppt", ".pptx") 
    $path = "T:\*"
    $docs = Get-ChildItem -Path $path -Recurse | ?{$includeExtensions -contains $_.Extension}
    
    foreach($doc in $docs) {
        $application = New-Object -ComObject powerpoint.application
        $document = $application.presentations.open($doc.FullName)
        $binding = "System.Reflection.BindingFlags" -as [type]
        $properties = $document.BuiltInDocumentProperties
    
        $lastsavetime = $null
        $creationdate = $null
    
        foreach($property in $properties)
        {
         $pn = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null)
          trap [system.exception]
           {
            continue
           }
           if($pn -eq "Last save time") {
                $lastsavetime = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
           } elseif ($pn -eq "Creation date") {
                $creationdate = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
           }                
        }
    
        $document.close()
        $application.quit()
    
        "Setting " + $doc.FullName
        Set-ItemProperty $doc.FullName -Name "Creationtime" -Value $creationdate 
        Set-ItemProperty $doc.FullName -Name "LastWriteTime" -Value $lastsavetime 
    
    }

    And finally Excel workbooks:

    $includeExtensions = @(".xls", ".xlsx") 
    $path = "D:\SharePointBackup\Sales and Merchandising\*"
    $docs = Get-ChildItem -Path $path -Recurse | ?{$includeExtensions -contains $_.Extension}
    
    foreach($doc in $docs) {
        $application = New-Object -ComObject excel.application
        $application.Visible = $false
        $document = $application.workbooks.open($doc.FullName)
            
        $binding = "System.Reflection.BindingFlags" -as [type]
        $properties = $document.BuiltInDocumentProperties
    
        $lastsavetime = $null
        $creationdate = $null
    
        foreach($property in $properties)
        {
         $pn = [System.__ComObject].invokemember("name",$binding::GetProperty,$null,$property,$null)
          trap [system.exception]
           {
            continue
           }
           if($pn -eq "Last save time") {
                $lastsavetime = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
           } elseif ($pn -eq "Creation date") {
                $creationdate = [System.__ComObject].invokemember("value",$binding::GetProperty,$null,$property,$null)
           }                
        }
       # $saveFormat = [Enum]::Parse([Microsoft.Office.Interop.Excel.XlFileFormat],”xlWorkbookDefault”);
        
        #$savename = ($doc.FullName).substring(0,($doc.FullName).lastindexOf(“.”))
        #$document.Convert()
        #$document.saveas([ref]”$savename”, [ref]$saveFormat);
    
    #    $prevname = $savename + ".xls"
     #   $savename = $savename + ".xlsx"
     #
        $document.Close($false)
        $application.quit()
       $savename = $doc.fullname
        "Setting " + $savename
        Set-ItemProperty $savename -Name "LastAccessTime" -Value $lastsavetime
        Set-ItemProperty $savename -Name "Creationtime" -Value $creationdate 
        Set-ItemProperty $savename -Name "LastWriteTime" -Value $lastsavetime
    
        #if((Test-Path $savename) -eq $true -And (Test-Path $prevname) -eq $true) {
        #    "Removing " + $prevname
        #    Remove-Item $prevname -Confirm:$false
    
     #   }
    
    }
    

    You can see a few references to upgrading the XLS, DOC, and PPT files to the newer format. The script was only tested on Word Docs, but it worked perfectly.

    I hope this helps someone!

    • Marked as answer by robertlansing Friday, April 22, 2016 3:52 AM
    Friday, April 22, 2016 3:48 AM