由于您-Recurse
在列出文件时没有使用,您可以只使用文件名而不是添加相对路径属性:
$folder1 = gci 'C:\Users\username\desktop' -exclude *.pst,*.ost,*.iso,*.lnk
$folder2 = gci 'D:\desktop' -exclude *.pst,*.ost,*.iso,*.lnk
Compare-Object $folder1 $folder2 -Property Name,Length
Name Length SideIndicator
---- ------ -------------
test.txt 174 =>
test.csv 174 <=
# Alternatively, use -PassThru to keep the whole object:
Compare-Object $folder1 $folder2 -Property Name,Length -PassThru | select SideIndicator,FullName,Length,LastWriteTime
SideIndicator FullName Length LastWriteTime
------------- -------- ------ -------------
=> D:\desktop 174 7/14/2021 2:47:09 PM
<= C:\Users\username\desktop 174 7/14/2021 2:47:09 PM
用于Out-File -Append
将输出附加到文件。
至于对当前脚本进行故障排除,请尝试手动检查 RelativePath 属性是否看起来为您正确设置:
(Get-Directories $Folder3).RelativePath
(Get-Directories $Folder4).RelativePath
最后,我建议在 powershell 上使用 robocopy 来进行此类备份,因为它可以使用备份权限(对于锁定的文件)并且可以一次复制多个文件,但这是个人喜好:
robocopy source destination /b /mir /mt /r:0 /w:0
/b - Runs robocopy in backup mode. Will copy everything as long as you are an Administrator
/mir - Mirrors everything from the source to the destination
/mt - Copies up to 8 files at a time
/r:0 - Sets it to not retry a file, default is like a million retries
/w:0 - Sets the time to 0 seconds between retries - default is like 30 seconds
来源:https ://community.spiceworks.com/topic/286190-transfer-user-profiles-using-robocopy