Need help on Powershell Copy-Item from network drives
Asked Answered
M

3

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.

Merrythought answered 1/2, 2013 at 19:45 Comment(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?Tasty
H
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\"
Horseflesh answered 1/2, 2013 at 20:26 Comment(6)
The last bit, prepending Microsoft.PowerShell.Core\FileSystem::, worked for me. Thanks.Squiffy
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.Sevenup
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!Horseflesh
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.....Hysterectomize
@Hysterectomize 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.Horseflesh
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.Empire
M
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.

Mesothorax answered 3/11, 2016 at 20:11 Comment(0)
F
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#####
Febrifugal answered 16/10, 2021 at 3:38 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.