Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to copy my documents folder using powershell

Tags:

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.

like image 773
user6307678 Avatar asked Feb 11 '26 09:02

user6307678


1 Answers

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
like image 132
Adam Bertram Avatar answered Feb 15 '26 16:02

Adam Bertram



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!