简体   繁体   English

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

[英]Trying to copy my documents folder using powershell

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

I"m trying to copy my documents folder, however when I look in the backup folder I see that it copies my pictures and my videos as well. In addition to that I get the following errors:我正在尝试复制我的文档文件夹,但是当我查看备份文件夹时,我发现它也复制了我的图片和视频。除此之外,我还收到以下错误:

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

It also makes a user folder in the root of the selected drive.它还会在所选驱动器的根目录中创建一个用户文件夹。

This may work better for you.这可能更适合您。 You currently might be parsing sym links rather than getting the actual path.您目前可能正在解析符号链接而不是获取实际路径。 Using this method will get the actual folder path every time.使用这种方法每次都会得到实际的文件夹路径。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM