I try to move a folder with PowerShell
move-item c:\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)"
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With