I am getting an "illegal characters in path" error with the following code. Any thoughts?

$source = \\md\drive\MMM\<<DriveEnv>>\AA\Extracts\Active\filename.csv

$newfilename = \\md\drive\MMM\<<DriveEnv>>\AA\Extracts\Active\filename.csv.TEMP

Rename-Item $source -NewName $newfilename

1 Answer 1


Find the illegal characters and use Replace to strip them out in PowerShell. Below is an example how to replace < and > characters with NULL values which basically removes those characters.

It also appears that the << and >> are not be valid characters for a folder name in a UNC path. So you might replace those with the valid location or give an example to further test what that is actually set to in your example.

$source = "\\md\drive\MMM\<<DriveEnv>>\AA\Extracts\Active\filename.csv"

$source = $source.Replace("<","")
$source = $source.Replace(">","")

$newfilename = $source
$newfilename = $newfilename + ".TEMP"

Rename-Item $source -NewName $newfilename
  • The << and >> characters could work if you use the -literalPath parameter, which tells Powershell to use the path exactly as it was typed in.
    – Leathe
    Nov 26, 2015 at 8:14
  • It looks like @Mindy is trying to access a file from an external source (note the \\ in the front in the path). If that drive uses another filesystem, which supports < and >. IIRC ext3, at least, supports all bytes except \0 and null in the filename/foldername. That's what I mean :P
    – Leathe
    Nov 27, 2015 at 10:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .