如何使用Powershell在卷之间移动文件/文件夹?

问题描述

| 我尝试使用PowerShell移动文件
move-item  c:\\test c:\\test2
可行,但是
move-item c:\\test  \\\\192.168.1.50\\c$\\test2
并告诉我   Move-Item:源和目标   路径必须具有相同的根。移动   将无法跨卷工作。     

解决方法

如果
test
是目录,则它将不起作用,因为
Move-Item
的文档指出:   “ 3”将在同一提供程序支持的驱动器之间移动文件,但仅在同一驱动器中移动目录。 在这种情况下,可以使用
Copy-Item
,然后再使用
Remove-Item
try {
  Copy-Item -Recurse C:\\test \\\\192.168.1.50\\c$\\test2 -ErrorAction Stop
  Remove-Item -Recurse c:\\test
} catch {}
如果您不依赖PSDrives,另一种选择是简单地使用xcopy或robocopy。     ,这需要加强,可能应该使其成为一个函数,但是它可以工作。
$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)\"
  }
 }
}
    ,我有类似的问题。我发现在目标文件夹上,用户权限受到限制。我更改了对源驱动器和目标驱动器的完全许可权,并且迁移进展顺利。在move-item命令上没有相同的roots错误消息。