Download FTP Files

I need help with a script that downloads files from an FTP site using credentials. Everything works until I get to the DownloadFile method for the .NET.webclient. It throws an Exception: Illegal characters in path. error and I can’t figure out if it’s the source or the target and I’ve renamed the folders and files to eliminate spaces and dashes but I still get the same error.

Here’s my script:

Function Get-FTPFiles{


if($userid){$ftp.Credentials=New-Object System.Net.NetworkCredential($userid,$password)}
$reader=New-Object System.IO.StreamReader($strm,'UTF-8')
foreach($line in $lines){
$f=$line.Split(' ',[System.StringSplitOptions]::RemoveEmptyEntries)
$names = [string] "$($f[8]) $($f[9]) $($f[10]) $($f[11]) $($f[12]) $($f[13]) $($f[14])"
$dt=[datetime]"$($f[5]) $($f[6]) $($f[7])"
New-Object PSObject -Property $hash

foreach ($name in $names){
$source = "$uri"+$name
$target = '\\server\folder\subfolder\sub\sub2\'+$name
$wc = new-object

$uri2 = new-object system.uri($source)

#Download file from FTP server
#$wc.Credentials = new-object System.Net.NetworkCredential($userid, $password)
$wc.downloadfile($uri2, $target)
Catch [Net.WebException]{
$_|fl * -Force
#Delete files from FTP server after download
$request = [system.Net.FtpWebRequest]::Create($uri2)
$request.Method = [System.Net.WebRequestMethods+FTP]::DeleteFile
$response = $request.GetResponse()
"Deleted from FTP: $source" -f $response.StatusDescription



Thanks for the help.

I think it’s probably the source, based on the error text and what others have reported around the web. Â You should add some commands before you call DownloadFile that output the exact string for the Uri as well as the path. Â Write-Debug or Write-Verbose would be good commands to use, because then you could turn the verbose/debug output on or off with ease and leave them in should other issues arise later.

With these outputs, make sure there are no double-quotes surrounding the strings (or for the Uri that there are no double-quotes or other characters that are not allowed without escaping in a Uri in the string). Â I suspect something in that output will help highlight what the issue is here.

VERBOSE: - Turlington 22-28 NoVar April222013 1052.xlsx

Here’s the verbose, I’m not sure what “Illegal characters” it’s finding…?

The first verbose is the source and the next one is the Uri. I’ve tried removing all of the spaces, underscores, dashes and dots in the name but it still doesn’t make a difference.

Thanks for the reply.

Ok, I got it. I had to do a $names.Trim(). I guess there were some trailing characters that it didn’t like. Sorry about that.


It is good that you were able to find the issue and correct. Â For your benefit and that of others, I would like to share what I have come up with in the past to help find these types of errors quickly.

When you Write-Verbose/Write-Debug your strings (in this case a URI), I like to output those strings INSIDE quotes:

Write-Verbose -Message "The URI is "$uri"’

By doing this, if there are extra spaces that you don’t intend, you will see them show up inside the escaped quotation marks. Â Using this technique has saved me on countless occasions to find things that you might not normally see in the debug output.

Hope this helps you in the future.