如何使用Powershell跨卷移动文件/文件夹?

时间:2021-01-03 21:37:49

I try to move a folder with PowerShell

我尝试使用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.

Move-Item:源和目标路径必须具有相同的根。移动不会跨卷运行。

3 个解决方案

#1


24  

If test is a directory, it won't work, as the documentation for Move-Item states:

如果test是一个目录,它将无法工作,因为Move-Item的文档说明:

Move-Item will move files between drives that are supported by the same provider, but it will move directories only within the same drive.

Move-Item将在同一提供程序支持的驱动器之间移动文件,但它只会在同一驱动器中移动目录。

You can use Copy-Item followed by a Remove-Item in that case:

在这种情况下,您可以使用Copy-Item,后跟Remove-Item:

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.

如果你不依赖于PSDrives,另一种选择就是简单地使用xcopy或robocopy。

#2


4  

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)"
  }
 }
}

#3


1  

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.

我有类似的问题。我发现在目标文件夹上,用户权限有限。我更改了源和目标驱动器的完全权限,并且移动顺利进行。 move-item命令上没有相同的根错误消息。

#1


24  

If test is a directory, it won't work, as the documentation for Move-Item states:

如果test是一个目录,它将无法工作,因为Move-Item的文档说明:

Move-Item will move files between drives that are supported by the same provider, but it will move directories only within the same drive.

Move-Item将在同一提供程序支持的驱动器之间移动文件,但它只会在同一驱动器中移动目录。

You can use Copy-Item followed by a Remove-Item in that case:

在这种情况下,您可以使用Copy-Item,后跟Remove-Item:

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.

如果你不依赖于PSDrives,另一种选择就是简单地使用xcopy或robocopy。

#2


4  

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)"
  }
 }
}

#3


1  

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.

我有类似的问题。我发现在目标文件夹上,用户权限有限。我更改了源和目标驱动器的完全权限,并且移动顺利进行。 move-item命令上没有相同的根错误消息。