0

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:

  1. C:\test\testing\text.txt

  2. 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
14
  • Please post the exact error message you're receiving. Commented Feb 16, 2016 at 20:14
  • Sounds like you have a hidden character somewhere, but we would need the exception unmodified to get a better idea. Have you tried to use -LiteralPath as the source-parameter in Copy-Item? It usually works better with weird characters. Ex. Copy-Item -LiteralPath $File -Destination $newdirectory -Force Commented Feb 16, 2016 at 20:16
  • I just edited with the error I am receiving Commented Feb 16, 2016 at 20:18
  • I just tried with -LiteralPath and got the same error. Commented Feb 16, 2016 at 20:19
  • Does the input file contain any blank lines? Commented Feb 16, 2016 at 20:22

1 Answer 1

2

Out-String is putting something on the end of the string, so you need to trim it:

$newDirectory = (($Directory | Out-String) -replace "C:\\test", "C:\Backup").TrimEnd()

...or, remove Out-String--I'm not sure why you have it there:

$newDirectory = $Directory -replace "C:\\test", "C:\Backup"

You can see this by checking the length property:

PS C:\> $d = $Directory | out-string
PS C:\> $d.Length
17
PS C:\> $Directory.Length
15

If you want the script to create the subfolder under C:\Backup if it doesn't exist, place the following before the Copy-Item line:

if(!(test-path $newDirectory)){
    New-Item $newDirectory -type directory
}
Sign up to request clarification or add additional context in comments.

4 Comments

yeah I took out-string off, which allows it to copy but it's putting it into the root "Backup" folder instead of recreating the folder stucture
The copy-item command won't create the folder structure. If the correct specified directory doesn't already exist under c:\Backup, you will need to create it.
Thanks guys, I'll use xcopy or robo.
Yeah, as a general rule of thumb, xcopy and robocopy have a lot of code already written to handle a lot of things nicely, so if they can do the job it is usually just easier to call them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.