21

I am trying to use Copy-Item from remote machine to another remote machine with the command:

Copy-Item -Path "\\machine1\abc\123\log 1.zip" -Destination "\\machine2\\c$\Logs\"

I am constantly getting Error "Cannot find Path "\\machine1\abc\123\log 1.zip"

I can access that path and copy manually from there.

I am opening PowerCLI as administrator and running this script... I am absolutely stuck here and not sure how to resolve it.

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
Geeth
  • 321
  • 1
  • 2
  • 4
  • 1
    If you run `Get-ChildItem -Path "\\machine1\abc\123"` does it display an entry for `log 1.zip` or does it display an error? Also, is `log 1.zip` a hidden file? – Lance U. Matthews Feb 01 '13 at 20:07

3 Answers3

26

This seems to work as is on PowerShell v3. I don't have v2 handy to test with, but there are two options that I'm aware of, which ought to work. First, you could map PSDrives:

New-PSDrive -Name source -PSProvider FileSystem -Root \\machine1\abc\123 | Out-Null
New-PSDrive -Name target -PSProvider FileSystem -Root \\machine2\c$\Logs | Out-Null
Copy-Item -Path source:\log_1.zip -Destination target:
Remove-PSDrive source
Remove-PSDrive target

If this is something you're going to do a lot, you could even wrap this in a function:

Function Copy-ItemUNC($SourcePath, $TargetPath, $FileName)
{
   New-PSDrive -Name source -PSProvider FileSystem -Root $SourcePath | Out-Null
   New-PSDrive -Name target -PSProvider FileSystem -Root $TargetPath | Out-Null
   Copy-Item -Path source:\$FileName -Destination target:
   Remove-PSDrive source
   Remove-PSDrive target
}

Alternately, you can explicitly specify the provider with each path:

Copy-Item -Path "Microsoft.PowerShell.Core\FileSystem::\\machine1\abc\123\log 1.zip" -Destination "Microsoft.PowerShell.Core\FileSystem::\\machine2\\c$\Logs\"
KevinD
  • 3,023
  • 2
  • 22
  • 26
  • 7
    The last bit, prepending `Microsoft.PowerShell.Core\FileSystem::`, worked for me. Thanks. – blachniet Jul 25 '13 at 22:54
  • 2
    FYI, you can run Powershell 3 in v2 mode by running `powershell.exe -version 2`, you can verify this by checking the `$Host.Version` property. – qJake Oct 10 '13 at 18:19
  • Sweet! I didn't know you could do run v2 from within PowerShell 3. I've been testing scripts that need to run on v2 in a separate VM, so this tip will make things *much* easier. Thanks for the tip! – KevinD Oct 11 '13 at 18:18
  • Thank you, this solves my problem. Can anyone point me in the direction of an article explaining why New-PSDrive works and the bare UNC paths don't? My guess it's something to do with FileSystem permissions been visible correctly in PSDrive..... – MrEdmundo Oct 16 '14 at 13:03
  • @MrEdmundo I can't say for sure, but I suspect that it's because New-PSDrive is specifying that the provider to use is FileSystem. That's sort of like explicitly using the provider as part of the path, as in my last example. – KevinD Oct 31 '14 at 17:30
  • In case anyone was making the same mistake as I was when using this answer to copy from a local drive to UNC path, the `Microsoft.PowerShell.Core\FileSystem::` prefix should only be used on the UNC path, not the local drive. Otherwise you will get a `The given path's format is not supported.` error. – Gavin Ward Nov 24 '15 at 10:56
4

this works all day for me:

$strLFpath = "\\compname\e$\folder"
$strLFpath2 = "\\Remotecomputer\networkshare\remotefolder"  #this is a second option that also will work
$StrRLPath = "E:\localfolder"  
Copy-Item -Path "$StrRLPath\*" -Destination "$strLFpath" -Recurse -force -Verbose

things to watch: Copy-item define the LAST item as the object. for copying the content of a folder you NEED the \*

If you are copying the folder it self to a new location then you do not need to declare the content.

Kelly Davis
  • 354
  • 2
  • 6
0

I use this daily:

Robocopy /E \\\SOURCEIP\C$\123\  \\\DESTIP\C$\Logs\   
                                             

There is an empty space in the middle. For ROBCOPY, /E does a copy. You can google if you need to do a move.

Or:

$SourceIP = Read-Host "Enter the Source IP"

$DESTIP = Read-Host "Enter the Destination IP"

Robocopy /E \\\\$SourceIP\C$\123\  \\\\$DESTIP\C$\Logs\ 

####Just adjust the C$ path on both#####
ouflak
  • 2,458
  • 10
  • 44
  • 49