Wesley Tansey
Wesley Tansey

Reputation: 4605

Powershell Copy-Item fails on User\Documents with Access Denied

I have a simple backup script to copy the Documents folder to an external hard drive:

Copy-Item C:\Users\Username\Documents G:\Backup -Recurse -Force

However, when I run the command, Windows 8 is raising an UnauthorizedAccessException for the folders '...\Documents\My Music', '...\Documents\My Pictures', and '...\Documents\My Videos'. However, these folders are just symbolic links, which I assume is what is causing the error. How can I avoid copying symbolic links?

Edit: Even using a hard-coded Exclude list does not work. Full script is below:

$backupFolder = (get-date -uformat %Y%m%d)
$lines = Get-Content backup.cfg

$DirLinks = "C:\Users\Me\Documents\My Videos", "C:\Users\Me\Documents\My Music","C:\Users\Me\Documents\My Pictures"
$basePath = $lines[0] + $backupFolder + "\"
for ($i = 1; $i -lt $lines.Length; $i++)
{
    $lines[$i]
    Copy-Item $lines[$i] ($basePath + ($lines[$i].Replace(":", ""))) -Recurse -Force -Exclude $DirLinks
}

"Done!"

I still get the following errors:

    Copy-Item : Access to the path 'C:\Users\Me\Documents\My Music' is denied.
At C:\Users\Me\Desktop\backup app\backup.ps1:20 char:5
+     Copy-Item $lines[$i] ($basePath + ($lines[$i].Replace(":", ""))) -Recurse -F ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (My Music:DirectoryInfo) [Copy-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Access to the path 'C:\Users\Me\Documents\My Pictures' is denied.
At C:\Users\Me\Desktop\backup app\backup.ps1:20 char:5
+     Copy-Item $lines[$i] ($basePath + ($lines[$i].Replace(":", ""))) -Recurse -F ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (My Pictures:DirectoryInfo) [Copy-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

Copy-Item : Access to the path 'C:\Users\Me\Documents\My Videos' is denied.
At C:\Users\Me\Desktop\backup app\backup.ps1:20 char:5
+     Copy-Item $lines[$i] ($basePath + ($lines[$i].Replace(":", ""))) -Recurse -F ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (My Videos:DirectoryInfo) [Copy-Item], UnauthorizedAccessException
    + FullyQualifiedErrorId : CopyDirectoryInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand

Upvotes: 1

Views: 6174

Answers (2)

Rex Hardin
Rex Hardin

Reputation: 2189

Unfortunately, there's no simple way to avoid symbolic links.

First you need to find them:

$DirLinks = Get-ChildItem C:\SomeDir -Recurse | ? { $_.Attributes -like "*ReparsePoint*" } | % { $_.FullName } 

Then exclude them:

Copy-Item C:\SomeDir D:\Backup -Recurse -Force -Exclude $DirLinks

Upvotes: 2

Chadit
Chadit

Reputation: 965

Are you running the script as admin? Also I assume the user running the script is also the same user that would fit in the username area?

Upvotes: 0

Related Questions