0

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]:

torres
  • 1,283
  • 8
  • 21
  • 30
  • I change the $source to $source="M:\*" and now it's not complaining. – torres Jul 11 '12 at 02:27
  • I am now getting access denied on M:\Users directory. How to avoid this? This is my own hard drive from laptop. I need to retrieve all the files before I send it back to dell for replacement (under warranty). Please suggest. – torres Jul 11 '12 at 02:55
  • 1
    If this is an exercise in using Copy-Item, then this question is OK. If your goal is to recursively copy a directory from one place to another quickly, you should look at robocopy. – OldFart Jul 11 '12 at 15:13

1 Answers1

3
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
David Brabant
  • 41,623
  • 16
  • 83
  • 111
  • I get an error: An item with the specified name already exists, followed by 'Cannot overwrite the item with itself' – Asher May 21 '18 at 00:58
  • If you run into permission problems, try this: `powershell -ExecutionPolicy Bypass -File your_script.ps1` – JP Zhang Apr 19 '22 at 12:28