I receive the error message "Illegal Characters in path" when running the following simple script that utilizes the Copy-Item command:
$Files = Get-Content output.txt
ForEach($File in $Files){
echo $File
$Directory = Split-Path -Parent $File
$newDirectory = ($Directory | Out-String) -replace "C:\\test", "C:\Backup"
echo $newDirectory
Copy-Item $File $newDirectory -force -recurse
}
As you can see the $Files variable is pulling each line into an array. Each line is actually a file path and name. The echo outputs look fine and are below:
C:\test\testing\text.txt
C:\Backup\testing
The first is the orginal file location that is to be copied, the second is the folder to copy it into. Can anyone help me figure out what the "Illegal character" is in these two paths. The error points to the source path.
Full Error Code is below:
Copy-Item : Illegal characters in path.
At C:\users\lane.pulcini\desktop\searchfiles\testcopy.ps1:7 char:10
+ Copy-Item <<<< $File $newDirectory -force -recurse
+ CategoryInfo : NotSpecified: (:) [Copy-Item], ArgumentException
+ FullyQualifiedErrorId : S System.ArgumentException,Microsoft.PowerShell.Commands.CopyItemCommand
-LiteralPathas the source-parameter inCopy-Item? It usually works better with weird characters. Ex.Copy-Item -LiteralPath $File -Destination $newdirectory -Force