powershell copy-item从硬盘驱动器到另一个硬盘驱动器的所有文件

时间:2012-07-11 02:07:07

标签: powershell copy-item

这就是我所拥有的,但不断出现以下错误。我使用的是Windows 7家庭高级版64位。我需要将笔记本电脑硬盘中的所有内容复制到桌面K:\ Mybackup文件夹。

$source = "M:\"
$dest = "K:\MyBackup"
Copy-item $source $dest -recurse

PS C:> copy-item $ source $ dest Copy-Item:不支持给定路径的格式。 在行:1 char:10 + copy-item<<<< $ source $ dest     + CategoryInfo:InvalidOperation:(K:\ gateway \ M:\:String)[Copy-Item],NotSupportedException     + FullyQualifiedErrorId:ItemExistsNotSupportedError,Microsoft.PowerShell.Commands.CopyItemCommand

PS C:>拷贝项目

命令管道位置1处的cmdlet Copy-Item 提供以下参数的值: 路径[0]:

1 个答案:

答案 0 :(得分:2)

function Copy-Directories 
{
    param (
        [parameter(Mandatory = $true)] [string] $source,
        [parameter(Mandatory = $true)] [string] $destination        
    )

    try
    {
        Get-ChildItem -Path $source -Recurse -Force |
            Where-Object { $_.psIsContainer } |
            ForEach-Object { $_.FullName -replace [regex]::Escape($source), $destination } |
            ForEach-Object { $null = New-Item -ItemType Container -Path $_ }

        Get-ChildItem -Path $source -Recurse -Force |
            Where-Object { -not $_.psIsContainer } |
            Copy-Item -Force -Destination { $_.FullName -replace [regex]::Escape($source), $destination }
    }

    catch
    {
        Write-Host "$_"
    }
}

$source = "M:\"
$dest = "K:\MyBackup"

Copy-Directories $source $dest