Issue in Copy-Item in powershell

1.1k Views Asked by At

I used the Copy-Item command to copy a set reports to a server location. It was working fine before but recently more reports have been added to the folder and now most of the times it works fine but in some cases it shows the error:

The target file "$destination" is a directory, not a file

The code I used is:

Copy-Item -Path "$Source" -Destination "$destination" -Recurse -Force

I am not sure why I am not getting this error for every case.

1

There are 1 best solutions below

1
On

What you have should work but this may work around the issue if there is some limitation with certain shared folder destinations:

Note: Remove the -WhatIf once you confirm the changes it will make are correct. Currently it will only output what files would have been copied.

Get-ChildItem $Source | Foreach-Object {
  Copy-Item $_.FullName $destination -Recurse -Force -WhatIf
}

Basically, this enumerates all of the files and folders under the $Source directory, then copies the files or folders individually to the destination.


If you are able to offer the values of $Source and $Destination I or another might be able to offer a solution to your problem, rather than a workaround.