How do you move files/folders across volumes with Powershell?
I try to move a folder with PowerShell
move-item c:开发者_Go百科\test c:\test2
works, but
move-item c:\test \\192.168.1.50\c$\test2
does not and tells me
Move-Item : Source and destination path must have identical roots. Move will not work across volumes.
If test
is a directory, it won't work, as the documentation for Move-Item
states:
Move-Item
will move files between drives that are supported by the same provider, but it will move directories only within the same drive.
You can use Copy-Item
followed by a Remove-Item
in that case:
try {
Copy-Item -Recurse C:\test \\192.168.1.50\c$\test2 -ErrorAction Stop
Remove-Item -Recurse c:\test
} catch {}
Another option, if you don't rely on PSDrives, would be to simply use xcopy or robocopy.
This needs to be tightened up and should probably be made into a function but it works.
$source = "<UNC Path>"
$destination = "<UNC Path>"
if (test-path $destination -PathType Container)
{
foreach ( $srcObj in (get-childitem $source ))
{
$srcObjPath = "$($srcObj.fullname)"
$destObjPath = "$($destination)\$($srcObj.name)"
If((Test-Path -LiteralPath $destination))
{
copy-item $srcObjPath $destination -recurse
if ( (Test-Path -Path $destObjPath ) -eq $true)
{
if ( (compare-object (gci $srcObjPath -recurse) (gci $destObjPath -recurse)) -eq $null)
{
write-output "Compare is good. Remove $($srcObjPath)"
remove-item $srcObjPath -recurse
}
else
{
write-output "Compare is bad. Remove $($destObjPath)"
remove-item $destObjPath -recurse
}
}
else
{
write-output "$($destination) path is bad"
}
}
else
{
write-output "bad destinaton: $($destination)"
}
}
}
I had similar issue. I found out that on destination folder, user permissions were limited. I changed full permissions on source and destination drive and move went smoothly. No identical roots error message on move-item command.
精彩评论