Copy item to PowerShell; This path format is not supported.

I'm relatively new to PowerShell, and I tried to copy files using a text file formatted as follows:

file1.pdf
dir1\dir2\dir3\dir 4

file2.pdf
dir1\dir5\dir7\di r8

file3.pdf
...etc.

If the first line of each record is the file name, and the second is the file path from C: \ Users. For example, the full path to the first entry in the file:

C:\Users\dir1\dir2\dir3\dir 4\file1.pdf

Below is the code that I have, but I get an error: "This path format is not supported." and another mistake after that tells me that he cannot find the path that I guess is the result of the first error. I played a little with him, and I get the impression that this has something to do with passing the Copy-Item string.

    $file = Get-Content C:\Users\AJ\Desktop\gdocs.txt
    for ($i = 0; $i -le $file.length - 1; $i+=3)
    {
        $copyCommand = "C:\Users\" + $file[$i+1] + "\" + $file[$i] 
        $copyCommand = $copyCommand +  " C:\Users\AJ\Desktop\gdocs\"
        $copyCommand
        Copy-Item $copyCommand

    }
+3
source share
2 answers

, , , .

$to = "C:\Users\AJ\Desktop\gdocs\"

Get-Content C:\Users\AJ\Desktop\gdocs.txt -ReadCount 3 | foreach-object{
    $from = "C:\Users\" + (join-path $_[1] $_[0] )
    Copy-Item -Path $from -Destination $to
}
+5

( ):

$from = "C:\Users\" + $file[$i+1] + "\" + $file[$i] 
$to = "C:\Users\AJ\Desktop\gdocs\"
Copy-Item $from $to

$from $to Copy-Item cmdlet. -Path -Destinattion. :

Trace-Command -pshost -name parameterbinding { 
   Copy-Item $from $to
}
+1

All Articles