尝试使用PowerShell复制我的文档文件夹

时间:2016-05-08 20:17:41

标签: powershell

$dir = Read-Host 'Please enter the letter drive to backup user folder.'
copy-item ($env:USERPROFILE+"\Documents") -destination ($dir+":\Backup")   -recurse

我正在尝试复制我的文档文件夹,但是当我查看备份文件夹时,我发现它还会复制我的图片和我的视频。除此之外,我还会收到以下错误:

Copy-Item : Access to the path 'C:\Users\admin\Documents\My Music' is denied.
At C:\Users\admin\Desktop\Untitled1.ps1:2 char:10
+ copy-item <<<<  ($env:USERPROFILE+"\Documents") -destination ($dir+":\Backup") -recurse
+ CategoryInfo          : PermissionDenied: (My Music:DirectoryInfo) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Access to the path 'C:\Users\admin\Documents\My Pictures' is denied.
At C:\Users\admin\Desktop\Untitled1.ps1:2 char:10
+ copy-item <<<<  ($env:USERPROFILE+"\Documents") -destination ($dir+":\Backup") -recurse
+ CategoryInfo          : PermissionDenied: (My Pictures:DirectoryInfo) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Access to the path 'C:\Users\admin\Documents\My Videos' is denied.
At C:\Users\admin\Desktop\Untitled1.ps1:2 char:10
+ copy-item <<<<  ($env:USERPROFILE+"\Documents") -destination ($dir+":\Backup") -recurse
+ CategoryInfo          : PermissionDenied: (My Videos:DirectoryInfo) [Copy-Item], UnauthorizedAccessException
+ FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

它还会在所选驱动器的根目录中创建一个用户文件夹。

1 个答案:

答案 0 :(得分:1)

这可能对您更有效。您当前可能正在解析sym链接而不是获取实际路径。使用此方法每次都会获得实际的文件夹路径。

$dir = Read-Host 'Please enter the letter drive to backup user folder.'
copy-item -Path [environment]::getfolderpath('mydocuments') -destination ($dir+":\Backup") -Recurse