I'm copying the files from source site to destination site through the power shell using the below code.

Now, my requirement is to exclude the files from source site in copying which were not accessed in last 2 yrs or 3 yrs.

Can any one help me on this please?

Here is my code

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Function Copy-AllFilesWithMetadata
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $SourceFolder,
        [Parameter(Mandatory=$true)] [Microsoft.SharePoint.Client.Folder] $TargetFolder
    Try {
        #Get all Files from the source folder
        $SourceFilesColl = $SourceFolder.Files

        #Iterate through each file and copy
        Foreach($SourceFile in $SourceFilesColl)
            #Get the source file
            $FileInfo = [Microsoft.SharePoint.Client.File]::OpenBinaryDirect($SourceFolder.Context, $SourceFile.ServerRelativeUrl)

            #Copy File to the Target location
            $TargetFileURL = $TargetFolder.ServerRelativeUrl+"/"+$SourceFile.Name
            [Microsoft.SharePoint.Client.File]::SaveBinaryDirect($TargetFolder.Context, $TargetFileURL, $FileInfo.Stream,$True)

            #Copy Metadata field values
            $SourceListItem = $SourceFile.ListItemAllFields

            #Get the new file created
            $TargetFile = $TargetFolder.Context.Web.GetFileByServerRelativeUrl($TargetFileURL)
            $TargetListItem = $TargetFile.ListItemAllFields

            #Set Metadata values from the source
            $Author =$TargetFolder.Context.web.EnsureUser($SourceListItem["Author"].Email)
            $TargetListItem["Author"] = $Author
            $Editor =$TargetFolder.Context.web.EnsureUser($SourceListItem["Editor"].Email)
            $TargetListItem["Editor"] = $Editor
            $TargetListItem["Created"] = $SourceListItem["Created"]
            $TargetListItem["Modified"] = $SourceListItem["Modified"]

            Write-host -f Green "Copied File '$($SourceFile.ServerRelativeUrl)' to '$TargetFileURL'"

        #Process Sub Folders
        $SubFolders = $SourceFolder.Folders
        Foreach($SubFolder in $SubFolders)
            If($SubFolder.Name -ne "Forms")
                #Prepare Target Folder
                $TargetFolderURL = $SubFolder.ServerRelativeUrl -replace $SourceLibrary.RootFolder.ServerRelativeUrl, $TargetLibrary.RootFolder.ServerRelativeUrl
                Try {
                catch {
                        #Create Folder
                            Write-host "Folder Added:"$SubFolder.Name -f Yellow
                #Call the function recursively
                Copy-AllFilesWithMetadata -SourceFolder $SubFolder -TargetFolder $Folder
    Catch {
        write-host -f Red "Error Copying File!" $_.Exception.Message

#Set Parameter values
$SourceSiteURL= Read-Host -Prompt 'Enter Source Site URL'
$TargetSiteURL= Read-Host -Prompt 'Enter Target Site URL'

$SourceLibraryName= Read-Host -Prompt 'Enter Source Library Name'
$TargetLibraryName= Read-Host -Prompt 'Enter Target Library Name'

#Setup Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

#Setup the contexts
$SourceCtx = New-Object Microsoft.SharePoint.Client.ClientContext($SourceSiteURL)
$SourceCtx.Credentials = $Credentials
$TargetCtx = New-Object Microsoft.SharePoint.Client.ClientContext($TargetSiteURL)
$TargetCtx.Credentials = $Credentials

$SourceCtx.RequestTimeOut = 5000*10000
$TargetCtx.RequestTimeOut = 5000*10000

#Get the source library and Target Libraries
$SourceLibrary = $SourceCtx.Web.Lists.GetByTitle($SourceLibraryName)

$TargetLibrary = $TargetCtx.Web.Lists.GetByTitle($TargetLibraryName)

#Call the function
Copy-AllFilesWithMetadata -SourceFolder $SourceLibrary.RootFolder -TargetFolder $TargetLibrary.RootFolder

1 Answer 1


Use sharegate migration tool, if you don't have, you can download the trial version of the tool.

There you can generate the report like last modified x months or years. Export that report into a csv, from your powershell migration tool, exclude those files.

Moreover, if you plan to do the migration using the third party tool like sharegate or Metalogix - you can apply the rules in the migration tool, so that old data will not be migrated.

  • Hi Rahaman, thanks for your reply, but i prefer to do with powershell script instead of Migration tool
    – taruki_MS
    Apr 16, 2020 at 10:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.