torres
torres

Reputation: 1353

powershell copy-item all files from harddrive to another harddrive

This is what I have but keep getting following error. I am using Windows 7 Home Premium 64bit. I need to copy everything from my laptop hard-drive to desktop K:\Mybackup folder.

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

PS C:> copy-item $source $dest Copy-Item : The given path's format is not supported. At line:1 char:10 + copy-item <<<< $source $dest + CategoryInfo : InvalidOperation: (K:\gateway\M:\:String) [Copy-Item], NotSupportedException + FullyQualifiedErrorId : ItemExistsNotSupportedError,Microsoft.PowerShell.Commands.CopyItemCommand

PS C:> copy-item

cmdlet Copy-Item at command pipeline position 1 Supply values for the following parameters: Path[0]:

Upvotes: 0

Views: 13374

Answers (1)

David Brabant
David Brabant

Reputation: 43559

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

Upvotes: 3

Related Questions