MrPew
MrPew

Reputation: 11

Copy-Items: Copying the same folder more than once

I try to copy the files and folder that is last access 1 year ago and this is my script:

$Source = "\\UNC\Path\Folder\"
$Dest = "\\UNC\Path2\Folder\1"

$Get = Get-ChildItem $Source -Recurse | where {
    $_.LastAccessTime -ge (Get-Date).AddMonths(-12).ToString("yyyy-MM-dd")
} 
$Get | Copy-Item -Destination $Dest -Recurse 

The script works except it copies the files and folder more than once.

For example, it will copy \\UNC\Path\Folder\a\b\File1.txt to both:

Note it skips the folder called 1 and puts it directly under Folder.

Now the File1.txt have been copied twice and it's the same file, just different dest. location.

I have Googled and search this forum but I haven't found anything. Any idea what it might be?

Upvotes: 0

Views: 328

Answers (1)

MrPew
MrPew

Reputation: 11

I used RoboCopy as @TessellatingHeckler suggested and it work great!

Robocopy.exe \\Source\folder\Folder1 \\dest\folder\folder\1 /MAXLAD:365 /e /copyall /log:C:\Logs\log.txt

Robocopy source: https://social.technet.microsoft.com/wiki/contents/articles/1073.robocopy-and-a-few-examples.aspx#Copy_all_content_including_empty_directory

/maxlad: Specifies the maximum last access date (excludes files unused since N).

/minlad: Specifies the minimum last access date (excludes files used since N) If N is less than 1900, N specifies the number of days. Otherwise, N specifies a date in the format YYYYMMDD

Upvotes: 1

Related Questions