user6307678
user6307678

Reputation: 31

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.

Upvotes: 3

Views: 1760

Answers (1)

Adam Bertram
Adam Bertram

Reputation: 4188

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

Upvotes: 1

Related Questions