2
votes

I am using powershell to copy folder and files inside. Below is the command im using

Copy-Item "C:\source" "C:\destination" -recurse

i get error

Copy-Item : The specified path, file name, or both are too long. The fully qualified file name must be le characters, and the directory name must be less than 248 characters. At line:1 char:1 + Copy-Item "C:\source" "C:\destination" -recurse + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (component.json:FileInfo) [Copy-Item], PathTooLongException + FullyQualifiedErrorId : CopyDirectoryInfoItemIOError,Microsoft.PowerShell.Commands.CopyItemCommand

i understood the error, but i wanted to know which file is having issue so i can fix it. The Error doesnt show the actual file

I tried using

$error[0]|format-list -force

it shows detail stack trace but it doesn't show the file path

2
can you simply append -Verbose to your Copy-Item command, although it might be a lot of console noise I expect you would see the offending file just before the exception.ATek

2 Answers

0
votes

The $error array consists of ErrorRecords. You can see what properties and methods are in an ErrorRecord by: $error|get-member. Among other things you'll see the ErrorDetails, Exception, and TargetObject properties. In many cases one or more of those properties will have additional useful information.

In your case TargetObject is a good bet. I tried to copy a non-existent file on my system and TargetObject was set to the invalid file name. To view you can use: $error[0].TargetObject|select *.

0
votes

The $error array consists of ErrorRecords. You can see what properties and methods are in an ErrorRecord by: $error|get-member. Among other things you'll see the ErrorDetails, Exception, and TargetObject properties. In many cases one or more of those properties will have additional useful information.

In your case TargetObject is a good bet. I tried to copy a non-existent file on my system and TargetObject was set to the invalid file name. To view you can use: $error[0].TargetObject|select *.

I have a script in my profile that outputs $error[0] as well as some of those properties listed above. Sometimes $error[0].exception will have an "inner exception", so my script checks to see if that exists and dumps that as well.